added download 2
This commit is contained in:
-182
@@ -1,182 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.FunctionsClient = void 0;
|
||||
const tslib_1 = require("tslib");
|
||||
const helper_1 = require("./helper");
|
||||
const types_1 = require("./types");
|
||||
/**
|
||||
* Client for invoking Supabase Edge Functions.
|
||||
*/
|
||||
class FunctionsClient {
|
||||
/**
|
||||
* Creates a new Functions client bound to an Edge Functions URL.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { FunctionsClient, FunctionRegion } from '@supabase/functions-js'
|
||||
*
|
||||
* const functions = new FunctionsClient('https://xyzcompany.supabase.co/functions/v1', {
|
||||
* headers: { apikey: 'public-anon-key' },
|
||||
* region: FunctionRegion.UsEast1,
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
constructor(url, { headers = {}, customFetch, region = types_1.FunctionRegion.Any, } = {}) {
|
||||
this.url = url;
|
||||
this.headers = headers;
|
||||
this.region = region;
|
||||
this.fetch = (0, helper_1.resolveFetch)(customFetch);
|
||||
}
|
||||
/**
|
||||
* Updates the authorization header
|
||||
* @param token - the new jwt token sent in the authorisation header
|
||||
* @example
|
||||
* ```ts
|
||||
* functions.setAuth(session.access_token)
|
||||
* ```
|
||||
*/
|
||||
setAuth(token) {
|
||||
this.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
/**
|
||||
* Invokes a function
|
||||
* @param functionName - The name of the Function to invoke.
|
||||
* @param options - Options for invoking the Function.
|
||||
* @example
|
||||
* ```ts
|
||||
* const { data, error } = await functions.invoke('hello-world', {
|
||||
* body: { name: 'Ada' },
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
invoke(functionName_1) {
|
||||
return tslib_1.__awaiter(this, arguments, void 0, function* (functionName, options = {}) {
|
||||
var _a;
|
||||
let timeoutId;
|
||||
let timeoutController;
|
||||
try {
|
||||
const { headers, method, body: functionArgs, signal, timeout } = options;
|
||||
let _headers = {};
|
||||
let { region } = options;
|
||||
if (!region) {
|
||||
region = this.region;
|
||||
}
|
||||
// Add region as query parameter using URL API
|
||||
const url = new URL(`${this.url}/${functionName}`);
|
||||
if (region && region !== 'any') {
|
||||
_headers['x-region'] = region;
|
||||
url.searchParams.set('forceFunctionRegion', region);
|
||||
}
|
||||
let body;
|
||||
if (functionArgs &&
|
||||
((headers && !Object.prototype.hasOwnProperty.call(headers, 'Content-Type')) || !headers)) {
|
||||
if ((typeof Blob !== 'undefined' && functionArgs instanceof Blob) ||
|
||||
functionArgs instanceof ArrayBuffer) {
|
||||
// will work for File as File inherits Blob
|
||||
// also works for ArrayBuffer as it is the same underlying structure as a Blob
|
||||
_headers['Content-Type'] = 'application/octet-stream';
|
||||
body = functionArgs;
|
||||
}
|
||||
else if (typeof functionArgs === 'string') {
|
||||
// plain string
|
||||
_headers['Content-Type'] = 'text/plain';
|
||||
body = functionArgs;
|
||||
}
|
||||
else if (typeof FormData !== 'undefined' && functionArgs instanceof FormData) {
|
||||
// don't set content-type headers
|
||||
// Request will automatically add the right boundary value
|
||||
body = functionArgs;
|
||||
}
|
||||
else {
|
||||
// default, assume this is JSON
|
||||
_headers['Content-Type'] = 'application/json';
|
||||
body = JSON.stringify(functionArgs);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (functionArgs &&
|
||||
typeof functionArgs !== 'string' &&
|
||||
!(typeof Blob !== 'undefined' && functionArgs instanceof Blob) &&
|
||||
!(functionArgs instanceof ArrayBuffer) &&
|
||||
!(typeof FormData !== 'undefined' && functionArgs instanceof FormData)) {
|
||||
body = JSON.stringify(functionArgs);
|
||||
}
|
||||
else {
|
||||
body = functionArgs;
|
||||
}
|
||||
}
|
||||
// Handle timeout by creating an AbortController
|
||||
let effectiveSignal = signal;
|
||||
if (timeout) {
|
||||
timeoutController = new AbortController();
|
||||
timeoutId = setTimeout(() => timeoutController.abort(), timeout);
|
||||
// If user provided their own signal, we need to respect both
|
||||
if (signal) {
|
||||
effectiveSignal = timeoutController.signal;
|
||||
// If the user's signal is aborted, abort our timeout controller too
|
||||
signal.addEventListener('abort', () => timeoutController.abort());
|
||||
}
|
||||
else {
|
||||
effectiveSignal = timeoutController.signal;
|
||||
}
|
||||
}
|
||||
const response = yield this.fetch(url.toString(), {
|
||||
method: method || 'POST',
|
||||
// headers priority is (high to low):
|
||||
// 1. invoke-level headers
|
||||
// 2. client-level headers
|
||||
// 3. default Content-Type header
|
||||
headers: Object.assign(Object.assign(Object.assign({}, _headers), this.headers), headers),
|
||||
body,
|
||||
signal: effectiveSignal,
|
||||
}).catch((fetchError) => {
|
||||
throw new types_1.FunctionsFetchError(fetchError);
|
||||
});
|
||||
const isRelayError = response.headers.get('x-relay-error');
|
||||
if (isRelayError && isRelayError === 'true') {
|
||||
throw new types_1.FunctionsRelayError(response);
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new types_1.FunctionsHttpError(response);
|
||||
}
|
||||
let responseType = ((_a = response.headers.get('Content-Type')) !== null && _a !== void 0 ? _a : 'text/plain').split(';')[0].trim();
|
||||
let data;
|
||||
if (responseType === 'application/json') {
|
||||
data = yield response.json();
|
||||
}
|
||||
else if (responseType === 'application/octet-stream' ||
|
||||
responseType === 'application/pdf') {
|
||||
data = yield response.blob();
|
||||
}
|
||||
else if (responseType === 'text/event-stream') {
|
||||
data = response;
|
||||
}
|
||||
else if (responseType === 'multipart/form-data') {
|
||||
data = yield response.formData();
|
||||
}
|
||||
else {
|
||||
// default to text
|
||||
data = yield response.text();
|
||||
}
|
||||
return { data, error: null, response };
|
||||
}
|
||||
catch (error) {
|
||||
return {
|
||||
data: null,
|
||||
error,
|
||||
response: error instanceof types_1.FunctionsHttpError || error instanceof types_1.FunctionsRelayError
|
||||
? error.context
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
finally {
|
||||
// Clear the timeout if it was set
|
||||
if (timeoutId) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.FunctionsClient = FunctionsClient;
|
||||
//# sourceMappingURL=FunctionsClient.js.map
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.resolveFetch = void 0;
|
||||
const resolveFetch = (customFetch) => {
|
||||
if (customFetch) {
|
||||
return (...args) => customFetch(...args);
|
||||
}
|
||||
return (...args) => fetch(...args);
|
||||
};
|
||||
exports.resolveFetch = resolveFetch;
|
||||
//# sourceMappingURL=helper.js.map
|
||||
-12
@@ -1,12 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.FunctionRegion = exports.FunctionsRelayError = exports.FunctionsHttpError = exports.FunctionsFetchError = exports.FunctionsError = exports.FunctionsClient = void 0;
|
||||
var FunctionsClient_1 = require("./FunctionsClient");
|
||||
Object.defineProperty(exports, "FunctionsClient", { enumerable: true, get: function () { return FunctionsClient_1.FunctionsClient; } });
|
||||
var types_1 = require("./types");
|
||||
Object.defineProperty(exports, "FunctionsError", { enumerable: true, get: function () { return types_1.FunctionsError; } });
|
||||
Object.defineProperty(exports, "FunctionsFetchError", { enumerable: true, get: function () { return types_1.FunctionsFetchError; } });
|
||||
Object.defineProperty(exports, "FunctionsHttpError", { enumerable: true, get: function () { return types_1.FunctionsHttpError; } });
|
||||
Object.defineProperty(exports, "FunctionsRelayError", { enumerable: true, get: function () { return types_1.FunctionsRelayError; } });
|
||||
Object.defineProperty(exports, "FunctionRegion", { enumerable: true, get: function () { return types_1.FunctionRegion; } });
|
||||
//# sourceMappingURL=index.js.map
|
||||
-91
@@ -1,91 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.FunctionRegion = exports.FunctionsHttpError = exports.FunctionsRelayError = exports.FunctionsFetchError = exports.FunctionsError = void 0;
|
||||
/**
|
||||
* Base error for Supabase Edge Function invocations.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { FunctionsError } from '@supabase/functions-js'
|
||||
*
|
||||
* throw new FunctionsError('Unexpected error invoking function', 'FunctionsError', {
|
||||
* requestId: 'abc123',
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
class FunctionsError extends Error {
|
||||
constructor(message, name = 'FunctionsError', context) {
|
||||
super(message);
|
||||
this.name = name;
|
||||
this.context = context;
|
||||
}
|
||||
}
|
||||
exports.FunctionsError = FunctionsError;
|
||||
/**
|
||||
* Error thrown when the network request to an Edge Function fails.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { FunctionsFetchError } from '@supabase/functions-js'
|
||||
*
|
||||
* throw new FunctionsFetchError({ requestId: 'abc123' })
|
||||
* ```
|
||||
*/
|
||||
class FunctionsFetchError extends FunctionsError {
|
||||
constructor(context) {
|
||||
super('Failed to send a request to the Edge Function', 'FunctionsFetchError', context);
|
||||
}
|
||||
}
|
||||
exports.FunctionsFetchError = FunctionsFetchError;
|
||||
/**
|
||||
* Error thrown when the Supabase relay cannot reach the Edge Function.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { FunctionsRelayError } from '@supabase/functions-js'
|
||||
*
|
||||
* throw new FunctionsRelayError({ region: 'us-east-1' })
|
||||
* ```
|
||||
*/
|
||||
class FunctionsRelayError extends FunctionsError {
|
||||
constructor(context) {
|
||||
super('Relay Error invoking the Edge Function', 'FunctionsRelayError', context);
|
||||
}
|
||||
}
|
||||
exports.FunctionsRelayError = FunctionsRelayError;
|
||||
/**
|
||||
* Error thrown when the Edge Function returns a non-2xx status code.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { FunctionsHttpError } from '@supabase/functions-js'
|
||||
*
|
||||
* throw new FunctionsHttpError({ status: 500 })
|
||||
* ```
|
||||
*/
|
||||
class FunctionsHttpError extends FunctionsError {
|
||||
constructor(context) {
|
||||
super('Edge Function returned a non-2xx status code', 'FunctionsHttpError', context);
|
||||
}
|
||||
}
|
||||
exports.FunctionsHttpError = FunctionsHttpError;
|
||||
// Define the enum for the 'region' property
|
||||
var FunctionRegion;
|
||||
(function (FunctionRegion) {
|
||||
FunctionRegion["Any"] = "any";
|
||||
FunctionRegion["ApNortheast1"] = "ap-northeast-1";
|
||||
FunctionRegion["ApNortheast2"] = "ap-northeast-2";
|
||||
FunctionRegion["ApSouth1"] = "ap-south-1";
|
||||
FunctionRegion["ApSoutheast1"] = "ap-southeast-1";
|
||||
FunctionRegion["ApSoutheast2"] = "ap-southeast-2";
|
||||
FunctionRegion["CaCentral1"] = "ca-central-1";
|
||||
FunctionRegion["EuCentral1"] = "eu-central-1";
|
||||
FunctionRegion["EuWest1"] = "eu-west-1";
|
||||
FunctionRegion["EuWest2"] = "eu-west-2";
|
||||
FunctionRegion["EuWest3"] = "eu-west-3";
|
||||
FunctionRegion["SaEast1"] = "sa-east-1";
|
||||
FunctionRegion["UsEast1"] = "us-east-1";
|
||||
FunctionRegion["UsWest1"] = "us-west-1";
|
||||
FunctionRegion["UsWest2"] = "us-west-2";
|
||||
})(FunctionRegion || (exports.FunctionRegion = FunctionRegion = {}));
|
||||
//# sourceMappingURL=types.js.map
|
||||
-1
@@ -1 +0,0 @@
|
||||
../../../.nitro/tslib@2.8.1
|
||||
-52
@@ -1,52 +0,0 @@
|
||||
{
|
||||
"name": "@supabase/functions-js",
|
||||
"version": "2.93.3",
|
||||
"description": "JS SDK to interact with Supabase Functions.",
|
||||
"main": "dist/main/index.js",
|
||||
"module": "dist/module/index.js",
|
||||
"types": "dist/module/index.d.ts",
|
||||
"sideEffects": false,
|
||||
"scripts": {
|
||||
"build": "npm run build:main && npm run build:module",
|
||||
"build:main": "tsc -p tsconfig.json",
|
||||
"build:module": "tsc -p tsconfig.module.json",
|
||||
"docs": "typedoc src/index.ts --out docs/v2",
|
||||
"docs:json": "typedoc --json docs/v2/spec.json --excludeExternals src/index.ts",
|
||||
"test": "jest",
|
||||
"test:ci": "jest --coverage"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/supabase/supabase-js.git",
|
||||
"directory": "packages/core/functions-js"
|
||||
},
|
||||
"keywords": [
|
||||
"functions",
|
||||
"supabase"
|
||||
],
|
||||
"author": "Supabase",
|
||||
"files": [
|
||||
"dist",
|
||||
"src"
|
||||
],
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/supabase/supabase-js/issues"
|
||||
},
|
||||
"homepage": "https://github.com/supabase/supabase-js/tree/master/packages/core/functions-js",
|
||||
"dependencies": {
|
||||
"tslib": "2.8.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"bs-logger": "^0.2.6",
|
||||
"nanoid": "^3.3.1",
|
||||
"openai": "^4.52.5",
|
||||
"testcontainers": "^8.5.1"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20.0.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user