@supabase/functions-js 1.3.2 → 1.4.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/main/FunctionsClient.d.ts +26 -0
- package/dist/main/FunctionsClient.d.ts.map +1 -0
- package/dist/main/FunctionsClient.js +104 -0
- package/dist/main/FunctionsClient.js.map +1 -0
- package/dist/main/helper.d.ts +1 -2
- package/dist/main/helper.d.ts.map +1 -1
- package/dist/main/helper.js +32 -4
- package/dist/main/helper.js.map +1 -1
- package/dist/main/index.d.ts +2 -30
- package/dist/main/index.d.ts.map +1 -1
- package/dist/main/index.js +8 -68
- package/dist/main/index.js.map +1 -1
- package/dist/main/types.d.ts +27 -13
- package/dist/main/types.d.ts.map +1 -1
- package/dist/main/types.js +27 -8
- package/dist/main/types.js.map +1 -1
- package/dist/main/version.d.ts +1 -1
- package/dist/main/version.js +1 -1
- package/dist/module/FunctionsClient.d.ts +26 -0
- package/dist/module/FunctionsClient.d.ts.map +1 -0
- package/dist/module/FunctionsClient.js +100 -0
- package/dist/module/FunctionsClient.js.map +1 -0
- package/dist/module/helper.d.ts +1 -2
- package/dist/module/helper.d.ts.map +1 -1
- package/dist/module/helper.js +10 -2
- package/dist/module/helper.js.map +1 -1
- package/dist/module/index.d.ts +2 -30
- package/dist/module/index.d.ts.map +1 -1
- package/dist/module/index.js +2 -66
- package/dist/module/index.js.map +1 -1
- package/dist/module/types.d.ts +27 -13
- package/dist/module/types.d.ts.map +1 -1
- package/dist/module/types.js +22 -7
- package/dist/module/types.js.map +1 -1
- package/dist/module/version.d.ts +1 -1
- package/dist/module/version.js +1 -1
- package/package.json +4 -4
- package/src/FunctionsClient.ts +115 -0
- package/src/helper.ts +2 -4
- package/src/index.ts +8 -74
- package/src/types.ts +37 -9
- package/src/version.ts +1 -1
package/README.md
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Fetch, FunctionsResponse } from './types';
|
|
2
|
+
export declare class FunctionsClient {
|
|
3
|
+
protected url: string;
|
|
4
|
+
protected headers: Record<string, string>;
|
|
5
|
+
protected fetch: Fetch;
|
|
6
|
+
constructor(url: string, { headers, customFetch, }?: {
|
|
7
|
+
headers?: Record<string, string>;
|
|
8
|
+
customFetch?: Fetch;
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* Updates the authorization header
|
|
12
|
+
* @params token - the new jwt token sent in the authorisation header
|
|
13
|
+
*/
|
|
14
|
+
setAuth(token: string): void;
|
|
15
|
+
/**
|
|
16
|
+
* Invokes a function
|
|
17
|
+
* @param functionName - the name of the function to invoke
|
|
18
|
+
* @param functionArgs - the arguments to the function
|
|
19
|
+
* @param options - function invoke options
|
|
20
|
+
* @param options.headers - headers to send with the request
|
|
21
|
+
*/
|
|
22
|
+
invoke(functionName: string, functionArgs: any, { headers, }?: {
|
|
23
|
+
headers?: Record<string, string>;
|
|
24
|
+
}): Promise<FunctionsResponse>;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=FunctionsClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FunctionsClient.d.ts","sourceRoot":"","sources":["../../src/FunctionsClient.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,EAIL,iBAAiB,EAClB,MAAM,SAAS,CAAA;AAEhB,qBAAa,eAAe;IAC1B,SAAS,CAAC,GAAG,EAAE,MAAM,CAAA;IACrB,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACzC,SAAS,CAAC,KAAK,EAAE,KAAK,CAAA;gBAGpB,GAAG,EAAE,MAAM,EACX,EACE,OAAY,EACZ,WAAW,GACZ,GAAE;QACD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;QAChC,WAAW,CAAC,EAAE,KAAK,CAAA;KACf;IAOR;;;OAGG;IACH,OAAO,CAAC,KAAK,EAAE,MAAM;IAIrB;;;;;;OAMG;IACG,MAAM,CACV,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,GAAG,EACjB,EACE,OAAY,GACb,GAAE;QACD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KAC5B,GACL,OAAO,CAAC,iBAAiB,CAAC;CA8D9B"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.FunctionsClient = void 0;
|
|
13
|
+
const helper_1 = require("./helper");
|
|
14
|
+
const types_1 = require("./types");
|
|
15
|
+
class FunctionsClient {
|
|
16
|
+
constructor(url, { headers = {}, customFetch, } = {}) {
|
|
17
|
+
this.url = url;
|
|
18
|
+
this.headers = headers;
|
|
19
|
+
this.fetch = (0, helper_1.resolveFetch)(customFetch);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Updates the authorization header
|
|
23
|
+
* @params token - the new jwt token sent in the authorisation header
|
|
24
|
+
*/
|
|
25
|
+
setAuth(token) {
|
|
26
|
+
this.headers.Authorization = `Bearer ${token}`;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Invokes a function
|
|
30
|
+
* @param functionName - the name of the function to invoke
|
|
31
|
+
* @param functionArgs - the arguments to the function
|
|
32
|
+
* @param options - function invoke options
|
|
33
|
+
* @param options.headers - headers to send with the request
|
|
34
|
+
*/
|
|
35
|
+
invoke(functionName, functionArgs, { headers = {}, } = {}) {
|
|
36
|
+
var _a;
|
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
try {
|
|
39
|
+
let _headers = {};
|
|
40
|
+
let body;
|
|
41
|
+
if (functionArgs instanceof Blob || functionArgs instanceof ArrayBuffer) {
|
|
42
|
+
// will work for File as File inherits Blob
|
|
43
|
+
// also works for ArrayBuffer as it is the same underlying structure as a Blob
|
|
44
|
+
_headers['Content-Type'] = 'application/octet-stream';
|
|
45
|
+
body = functionArgs;
|
|
46
|
+
}
|
|
47
|
+
else if (typeof functionArgs === 'string') {
|
|
48
|
+
// plain string
|
|
49
|
+
_headers['Content-Type'] = 'text/plain';
|
|
50
|
+
body = functionArgs;
|
|
51
|
+
}
|
|
52
|
+
else if (functionArgs instanceof FormData) {
|
|
53
|
+
// don't set content-type headers
|
|
54
|
+
// Request will automatically add the right boundary value
|
|
55
|
+
body = functionArgs;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
// default, assume this is JSON
|
|
59
|
+
_headers['Content-Type'] = 'application/json';
|
|
60
|
+
body = JSON.stringify(functionArgs);
|
|
61
|
+
}
|
|
62
|
+
const response = yield this.fetch(`${this.url}/${functionName}`, {
|
|
63
|
+
method: 'POST',
|
|
64
|
+
// headers priority is (high to low):
|
|
65
|
+
// 1. invoke-level headers
|
|
66
|
+
// 2. client-level headers
|
|
67
|
+
// 3. default Content-Type header
|
|
68
|
+
headers: Object.assign(Object.assign(Object.assign({}, _headers), this.headers), headers),
|
|
69
|
+
body,
|
|
70
|
+
}).catch((fetchError) => {
|
|
71
|
+
throw new types_1.FunctionsFetchError(fetchError);
|
|
72
|
+
});
|
|
73
|
+
const isRelayError = response.headers.get('x-relay-error');
|
|
74
|
+
if (isRelayError && isRelayError === 'true') {
|
|
75
|
+
throw new types_1.FunctionsRelayError(response);
|
|
76
|
+
}
|
|
77
|
+
if (!response.ok) {
|
|
78
|
+
throw new types_1.FunctionsHttpError(response);
|
|
79
|
+
}
|
|
80
|
+
let responseType = ((_a = response.headers.get('Content-Type')) !== null && _a !== void 0 ? _a : 'text/plain').split(';')[0].trim();
|
|
81
|
+
let data;
|
|
82
|
+
if (responseType === 'application/json') {
|
|
83
|
+
data = yield response.json();
|
|
84
|
+
}
|
|
85
|
+
else if (responseType === 'application/octet-stream') {
|
|
86
|
+
data = yield response.blob();
|
|
87
|
+
}
|
|
88
|
+
else if (responseType === 'multipart/form-data') {
|
|
89
|
+
data = yield response.formData();
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
// default to text
|
|
93
|
+
data = yield response.text();
|
|
94
|
+
}
|
|
95
|
+
return { data, error: null };
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
return { data: null, error };
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.FunctionsClient = FunctionsClient;
|
|
104
|
+
//# sourceMappingURL=FunctionsClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FunctionsClient.js","sourceRoot":"","sources":["../../src/FunctionsClient.ts"],"names":[],"mappings":"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"}
|
package/dist/main/helper.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../../src/helper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../../src/helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAE/B,eAAO,MAAM,YAAY,8CAA0B,KAUlD,CAAA"}
|
package/dist/main/helper.js
CHANGED
|
@@ -1,17 +1,45 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
4
33
|
};
|
|
5
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
35
|
exports.resolveFetch = void 0;
|
|
7
|
-
const cross_fetch_1 = __importDefault(require("cross-fetch"));
|
|
8
36
|
const resolveFetch = (customFetch) => {
|
|
9
37
|
let _fetch;
|
|
10
38
|
if (customFetch) {
|
|
11
39
|
_fetch = customFetch;
|
|
12
40
|
}
|
|
13
41
|
else if (typeof fetch === 'undefined') {
|
|
14
|
-
_fetch =
|
|
42
|
+
_fetch = (...args) => __awaiter(void 0, void 0, void 0, function* () { return yield (yield Promise.resolve().then(() => __importStar(require('cross-fetch')))).fetch(...args); });
|
|
15
43
|
}
|
|
16
44
|
else {
|
|
17
45
|
_fetch = fetch;
|
package/dist/main/helper.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../../src/helper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../../src/helper.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEO,MAAM,YAAY,GAAG,CAAC,WAAmB,EAAS,EAAE;IACzD,IAAI,MAAa,CAAA;IACjB,IAAI,WAAW,EAAE;QACf,MAAM,GAAG,WAAW,CAAA;KACrB;SAAM,IAAI,OAAO,KAAK,KAAK,WAAW,EAAE;QACvC,MAAM,GAAG,CAAO,GAAG,IAAI,EAAE,EAAE,kDAAC,OAAA,MAAM,CAAC,wDAAa,aAAa,GAAC,CAAC,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAA,GAAA,CAAA;KAC/E;SAAM;QACL,MAAM,GAAG,KAAK,CAAA;KACf;IACD,OAAO,CAAC,GAAG,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAA;AACrC,CAAC,CAAA;AAVY,QAAA,YAAY,gBAUxB"}
|
package/dist/main/index.d.ts
CHANGED
|
@@ -1,31 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
protected url: string;
|
|
4
|
-
protected headers: Record<string, string>;
|
|
5
|
-
protected fetch: Fetch;
|
|
6
|
-
constructor(url: string, { headers, customFetch, }: {
|
|
7
|
-
headers?: Record<string, string>;
|
|
8
|
-
customFetch?: Fetch;
|
|
9
|
-
});
|
|
10
|
-
/**
|
|
11
|
-
* Updates the authorization header
|
|
12
|
-
* @params token - the new jwt token sent in the authorisation header
|
|
13
|
-
*/
|
|
14
|
-
setAuth(token: string): void;
|
|
15
|
-
/**
|
|
16
|
-
* Invokes a function
|
|
17
|
-
* @param functionName - the name of the function to invoke
|
|
18
|
-
* @param invokeOptions - object with the following properties
|
|
19
|
-
* `headers`: object representing the headers to send with the request
|
|
20
|
-
* `body`: the body of the request
|
|
21
|
-
* `responseType`: how the response should be parsed. The default is `json`
|
|
22
|
-
*/
|
|
23
|
-
invoke<T = any>(functionName: string, invokeOptions?: FunctionInvokeOptions): Promise<{
|
|
24
|
-
data: T;
|
|
25
|
-
error: null;
|
|
26
|
-
} | {
|
|
27
|
-
data: null;
|
|
28
|
-
error: Error;
|
|
29
|
-
}>;
|
|
30
|
-
}
|
|
1
|
+
export { FunctionsClient } from './FunctionsClient';
|
|
2
|
+
export { FunctionsError, FunctionsFetchError, FunctionsHttpError, FunctionsRelayError, FunctionsResponse, } from './types';
|
|
31
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/main/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,kBAAkB,EAClB,mBAAmB,EACnB,iBAAiB,GAClB,MAAM,SAAS,CAAA"}
|
package/dist/main/index.js
CHANGED
|
@@ -1,71 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.FunctionsClient = void 0;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Updates the authorization header
|
|
22
|
-
* @params token - the new jwt token sent in the authorisation header
|
|
23
|
-
*/
|
|
24
|
-
setAuth(token) {
|
|
25
|
-
this.headers.Authorization = `Bearer ${token}`;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Invokes a function
|
|
29
|
-
* @param functionName - the name of the function to invoke
|
|
30
|
-
* @param invokeOptions - object with the following properties
|
|
31
|
-
* `headers`: object representing the headers to send with the request
|
|
32
|
-
* `body`: the body of the request
|
|
33
|
-
* `responseType`: how the response should be parsed. The default is `json`
|
|
34
|
-
*/
|
|
35
|
-
invoke(functionName, invokeOptions) {
|
|
36
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
-
try {
|
|
38
|
-
const { headers, body } = invokeOptions !== null && invokeOptions !== void 0 ? invokeOptions : {};
|
|
39
|
-
const response = yield this.fetch(`${this.url}/${functionName}`, {
|
|
40
|
-
method: 'POST',
|
|
41
|
-
headers: Object.assign({}, this.headers, headers),
|
|
42
|
-
body,
|
|
43
|
-
});
|
|
44
|
-
const isRelayError = response.headers.get('x-relay-error');
|
|
45
|
-
if (isRelayError && isRelayError === 'true') {
|
|
46
|
-
return { data: null, error: new Error(yield response.text()) };
|
|
47
|
-
}
|
|
48
|
-
let data;
|
|
49
|
-
const { responseType } = invokeOptions !== null && invokeOptions !== void 0 ? invokeOptions : {};
|
|
50
|
-
if (!responseType || responseType === 'json') {
|
|
51
|
-
data = yield response.json();
|
|
52
|
-
}
|
|
53
|
-
else if (responseType === 'arrayBuffer') {
|
|
54
|
-
data = yield response.arrayBuffer();
|
|
55
|
-
}
|
|
56
|
-
else if (responseType === 'blob') {
|
|
57
|
-
data = yield response.blob();
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
data = yield response.text();
|
|
61
|
-
}
|
|
62
|
-
return { data, error: null };
|
|
63
|
-
}
|
|
64
|
-
catch (error) {
|
|
65
|
-
return { data: null, error };
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
exports.FunctionsClient = FunctionsClient;
|
|
3
|
+
exports.FunctionsRelayError = exports.FunctionsHttpError = exports.FunctionsFetchError = exports.FunctionsError = exports.FunctionsClient = void 0;
|
|
4
|
+
var FunctionsClient_1 = require("./FunctionsClient");
|
|
5
|
+
Object.defineProperty(exports, "FunctionsClient", { enumerable: true, get: function () { return FunctionsClient_1.FunctionsClient; } });
|
|
6
|
+
var types_1 = require("./types");
|
|
7
|
+
Object.defineProperty(exports, "FunctionsError", { enumerable: true, get: function () { return types_1.FunctionsError; } });
|
|
8
|
+
Object.defineProperty(exports, "FunctionsFetchError", { enumerable: true, get: function () { return types_1.FunctionsFetchError; } });
|
|
9
|
+
Object.defineProperty(exports, "FunctionsHttpError", { enumerable: true, get: function () { return types_1.FunctionsHttpError; } });
|
|
10
|
+
Object.defineProperty(exports, "FunctionsRelayError", { enumerable: true, get: function () { return types_1.FunctionsRelayError; } });
|
|
71
11
|
//# sourceMappingURL=index.js.map
|
package/dist/main/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,qDAAmD;AAA1C,kHAAA,eAAe,OAAA;AACxB,iCAMgB;AALd,uGAAA,cAAc,OAAA;AACd,4GAAA,mBAAmB,OAAA;AACnB,2GAAA,kBAAkB,OAAA;AAClB,4GAAA,mBAAmB,OAAA"}
|
package/dist/main/types.d.ts
CHANGED
|
@@ -1,15 +1,29 @@
|
|
|
1
1
|
export declare type Fetch = typeof fetch;
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Response format
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
interface FunctionsResponseSuccess {
|
|
7
|
+
data: any;
|
|
8
|
+
error: null;
|
|
9
|
+
}
|
|
10
|
+
interface FunctionsResponseFailure {
|
|
11
|
+
data: null;
|
|
12
|
+
error: any;
|
|
13
|
+
}
|
|
14
|
+
export declare type FunctionsResponse = FunctionsResponseSuccess | FunctionsResponseFailure;
|
|
15
|
+
export declare class FunctionsError extends Error {
|
|
16
|
+
context: any;
|
|
17
|
+
constructor(message: string, name?: string, context?: any);
|
|
18
|
+
}
|
|
19
|
+
export declare class FunctionsFetchError extends FunctionsError {
|
|
20
|
+
constructor(context: any);
|
|
21
|
+
}
|
|
22
|
+
export declare class FunctionsRelayError extends FunctionsError {
|
|
23
|
+
constructor(context: any);
|
|
24
|
+
}
|
|
25
|
+
export declare class FunctionsHttpError extends FunctionsError {
|
|
26
|
+
constructor(context: any);
|
|
27
|
+
}
|
|
28
|
+
export {};
|
|
15
29
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/main/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,KAAK,GAAG,OAAO,KAAK,CAAA;AAEhC,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,KAAK,GAAG,OAAO,KAAK,CAAA;AAEhC;;;GAGG;AACH,UAAU,wBAAwB;IAChC,IAAI,EAAE,GAAG,CAAA;IACT,KAAK,EAAE,IAAI,CAAA;CACZ;AACD,UAAU,wBAAwB;IAChC,IAAI,EAAE,IAAI,CAAA;IACV,KAAK,EAAE,GAAG,CAAA;CACX;AACD,oBAAY,iBAAiB,GAAG,wBAAwB,GAAG,wBAAwB,CAAA;AAEnF,qBAAa,cAAe,SAAQ,KAAK;IACvC,OAAO,EAAE,GAAG,CAAA;gBACA,OAAO,EAAE,MAAM,EAAE,IAAI,SAAmB,EAAE,OAAO,CAAC,EAAE,GAAG;CAKpE;AAED,qBAAa,mBAAoB,SAAQ,cAAc;gBACzC,OAAO,EAAE,GAAG;CAGzB;AAED,qBAAa,mBAAoB,SAAQ,cAAc;gBACzC,OAAO,EAAE,GAAG;CAGzB;AAED,qBAAa,kBAAmB,SAAQ,cAAc;gBACxC,OAAO,EAAE,GAAG;CAGzB"}
|
package/dist/main/types.js
CHANGED
|
@@ -1,11 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
3
|
+
exports.FunctionsHttpError = exports.FunctionsRelayError = exports.FunctionsFetchError = exports.FunctionsError = void 0;
|
|
4
|
+
class FunctionsError extends Error {
|
|
5
|
+
constructor(message, name = 'FunctionsError', context) {
|
|
6
|
+
super(message);
|
|
7
|
+
super.name = name;
|
|
8
|
+
this.context = context;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.FunctionsError = FunctionsError;
|
|
12
|
+
class FunctionsFetchError extends FunctionsError {
|
|
13
|
+
constructor(context) {
|
|
14
|
+
super('Failed to perform request to Edge Function', 'FunctionsFetchError', context);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.FunctionsFetchError = FunctionsFetchError;
|
|
18
|
+
class FunctionsRelayError extends FunctionsError {
|
|
19
|
+
constructor(context) {
|
|
20
|
+
super('Relay error communicating with deno backend', 'FunctionsRelayError', context);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.FunctionsRelayError = FunctionsRelayError;
|
|
24
|
+
class FunctionsHttpError extends FunctionsError {
|
|
25
|
+
constructor(context) {
|
|
26
|
+
super('Edge Function returned a non-2xx status code', 'FunctionsHttpError', context);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.FunctionsHttpError = FunctionsHttpError;
|
|
11
30
|
//# sourceMappingURL=types.js.map
|
package/dist/main/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":";;;AAgBA,MAAa,cAAe,SAAQ,KAAK;IAEvC,YAAY,OAAe,EAAE,IAAI,GAAG,gBAAgB,EAAE,OAAa;QACjE,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,KAAK,CAAC,IAAI,GAAG,IAAI,CAAA;QACjB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;CACF;AAPD,wCAOC;AAED,MAAa,mBAAoB,SAAQ,cAAc;IACrD,YAAY,OAAY;QACtB,KAAK,CAAC,4CAA4C,EAAE,qBAAqB,EAAE,OAAO,CAAC,CAAA;IACrF,CAAC;CACF;AAJD,kDAIC;AAED,MAAa,mBAAoB,SAAQ,cAAc;IACrD,YAAY,OAAY;QACtB,KAAK,CAAC,6CAA6C,EAAE,qBAAqB,EAAE,OAAO,CAAC,CAAA;IACtF,CAAC;CACF;AAJD,kDAIC;AAED,MAAa,kBAAmB,SAAQ,cAAc;IACpD,YAAY,OAAY;QACtB,KAAK,CAAC,8CAA8C,EAAE,oBAAoB,EAAE,OAAO,CAAC,CAAA;IACtF,CAAC;CACF;AAJD,gDAIC"}
|
package/dist/main/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const version = "1.
|
|
1
|
+
export declare const version = "1.4.0-next.1";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/dist/main/version.js
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Fetch, FunctionsResponse } from './types';
|
|
2
|
+
export declare class FunctionsClient {
|
|
3
|
+
protected url: string;
|
|
4
|
+
protected headers: Record<string, string>;
|
|
5
|
+
protected fetch: Fetch;
|
|
6
|
+
constructor(url: string, { headers, customFetch, }?: {
|
|
7
|
+
headers?: Record<string, string>;
|
|
8
|
+
customFetch?: Fetch;
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* Updates the authorization header
|
|
12
|
+
* @params token - the new jwt token sent in the authorisation header
|
|
13
|
+
*/
|
|
14
|
+
setAuth(token: string): void;
|
|
15
|
+
/**
|
|
16
|
+
* Invokes a function
|
|
17
|
+
* @param functionName - the name of the function to invoke
|
|
18
|
+
* @param functionArgs - the arguments to the function
|
|
19
|
+
* @param options - function invoke options
|
|
20
|
+
* @param options.headers - headers to send with the request
|
|
21
|
+
*/
|
|
22
|
+
invoke(functionName: string, functionArgs: any, { headers, }?: {
|
|
23
|
+
headers?: Record<string, string>;
|
|
24
|
+
}): Promise<FunctionsResponse>;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=FunctionsClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FunctionsClient.d.ts","sourceRoot":"","sources":["../../src/FunctionsClient.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,EAIL,iBAAiB,EAClB,MAAM,SAAS,CAAA;AAEhB,qBAAa,eAAe;IAC1B,SAAS,CAAC,GAAG,EAAE,MAAM,CAAA;IACrB,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACzC,SAAS,CAAC,KAAK,EAAE,KAAK,CAAA;gBAGpB,GAAG,EAAE,MAAM,EACX,EACE,OAAY,EACZ,WAAW,GACZ,GAAE;QACD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;QAChC,WAAW,CAAC,EAAE,KAAK,CAAA;KACf;IAOR;;;OAGG;IACH,OAAO,CAAC,KAAK,EAAE,MAAM;IAIrB;;;;;;OAMG;IACG,MAAM,CACV,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,GAAG,EACjB,EACE,OAAY,GACb,GAAE;QACD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KAC5B,GACL,OAAO,CAAC,iBAAiB,CAAC;CA8D9B"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { resolveFetch } from './helper';
|
|
11
|
+
import { FunctionsFetchError, FunctionsHttpError, FunctionsRelayError, } from './types';
|
|
12
|
+
export class FunctionsClient {
|
|
13
|
+
constructor(url, { headers = {}, customFetch, } = {}) {
|
|
14
|
+
this.url = url;
|
|
15
|
+
this.headers = headers;
|
|
16
|
+
this.fetch = resolveFetch(customFetch);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Updates the authorization header
|
|
20
|
+
* @params token - the new jwt token sent in the authorisation header
|
|
21
|
+
*/
|
|
22
|
+
setAuth(token) {
|
|
23
|
+
this.headers.Authorization = `Bearer ${token}`;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Invokes a function
|
|
27
|
+
* @param functionName - the name of the function to invoke
|
|
28
|
+
* @param functionArgs - the arguments to the function
|
|
29
|
+
* @param options - function invoke options
|
|
30
|
+
* @param options.headers - headers to send with the request
|
|
31
|
+
*/
|
|
32
|
+
invoke(functionName, functionArgs, { headers = {}, } = {}) {
|
|
33
|
+
var _a;
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
try {
|
|
36
|
+
let _headers = {};
|
|
37
|
+
let body;
|
|
38
|
+
if (functionArgs instanceof Blob || functionArgs instanceof ArrayBuffer) {
|
|
39
|
+
// will work for File as File inherits Blob
|
|
40
|
+
// also works for ArrayBuffer as it is the same underlying structure as a Blob
|
|
41
|
+
_headers['Content-Type'] = 'application/octet-stream';
|
|
42
|
+
body = functionArgs;
|
|
43
|
+
}
|
|
44
|
+
else if (typeof functionArgs === 'string') {
|
|
45
|
+
// plain string
|
|
46
|
+
_headers['Content-Type'] = 'text/plain';
|
|
47
|
+
body = functionArgs;
|
|
48
|
+
}
|
|
49
|
+
else if (functionArgs instanceof FormData) {
|
|
50
|
+
// don't set content-type headers
|
|
51
|
+
// Request will automatically add the right boundary value
|
|
52
|
+
body = functionArgs;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
// default, assume this is JSON
|
|
56
|
+
_headers['Content-Type'] = 'application/json';
|
|
57
|
+
body = JSON.stringify(functionArgs);
|
|
58
|
+
}
|
|
59
|
+
const response = yield this.fetch(`${this.url}/${functionName}`, {
|
|
60
|
+
method: 'POST',
|
|
61
|
+
// headers priority is (high to low):
|
|
62
|
+
// 1. invoke-level headers
|
|
63
|
+
// 2. client-level headers
|
|
64
|
+
// 3. default Content-Type header
|
|
65
|
+
headers: Object.assign(Object.assign(Object.assign({}, _headers), this.headers), headers),
|
|
66
|
+
body,
|
|
67
|
+
}).catch((fetchError) => {
|
|
68
|
+
throw new FunctionsFetchError(fetchError);
|
|
69
|
+
});
|
|
70
|
+
const isRelayError = response.headers.get('x-relay-error');
|
|
71
|
+
if (isRelayError && isRelayError === 'true') {
|
|
72
|
+
throw new FunctionsRelayError(response);
|
|
73
|
+
}
|
|
74
|
+
if (!response.ok) {
|
|
75
|
+
throw new FunctionsHttpError(response);
|
|
76
|
+
}
|
|
77
|
+
let responseType = ((_a = response.headers.get('Content-Type')) !== null && _a !== void 0 ? _a : 'text/plain').split(';')[0].trim();
|
|
78
|
+
let data;
|
|
79
|
+
if (responseType === 'application/json') {
|
|
80
|
+
data = yield response.json();
|
|
81
|
+
}
|
|
82
|
+
else if (responseType === 'application/octet-stream') {
|
|
83
|
+
data = yield response.blob();
|
|
84
|
+
}
|
|
85
|
+
else if (responseType === 'multipart/form-data') {
|
|
86
|
+
data = yield response.formData();
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
// default to text
|
|
90
|
+
data = yield response.text();
|
|
91
|
+
}
|
|
92
|
+
return { data, error: null };
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
return { data: null, error };
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=FunctionsClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FunctionsClient.js","sourceRoot":"","sources":["../../src/FunctionsClient.ts"],"names":[],"mappings":"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"}
|
package/dist/module/helper.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../../src/helper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../../src/helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAE/B,eAAO,MAAM,YAAY,8CAA0B,KAUlD,CAAA"}
|
package/dist/module/helper.js
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
2
10
|
export const resolveFetch = (customFetch) => {
|
|
3
11
|
let _fetch;
|
|
4
12
|
if (customFetch) {
|
|
5
13
|
_fetch = customFetch;
|
|
6
14
|
}
|
|
7
15
|
else if (typeof fetch === 'undefined') {
|
|
8
|
-
_fetch =
|
|
16
|
+
_fetch = (...args) => __awaiter(void 0, void 0, void 0, function* () { return yield (yield import('cross-fetch')).fetch(...args); });
|
|
9
17
|
}
|
|
10
18
|
else {
|
|
11
19
|
_fetch = fetch;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../../src/helper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../../src/helper.ts"],"names":[],"mappings":";;;;;;;;;AAEA,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,WAAmB,EAAS,EAAE;IACzD,IAAI,MAAa,CAAA;IACjB,IAAI,WAAW,EAAE;QACf,MAAM,GAAG,WAAW,CAAA;KACrB;SAAM,IAAI,OAAO,KAAK,KAAK,WAAW,EAAE;QACvC,MAAM,GAAG,CAAO,GAAG,IAAI,EAAE,EAAE,kDAAC,OAAA,MAAM,CAAC,MAAM,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAA,GAAA,CAAA;KAC/E;SAAM;QACL,MAAM,GAAG,KAAK,CAAA;KACf;IACD,OAAO,CAAC,GAAG,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAA;AACrC,CAAC,CAAA"}
|
package/dist/module/index.d.ts
CHANGED
|
@@ -1,31 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
protected url: string;
|
|
4
|
-
protected headers: Record<string, string>;
|
|
5
|
-
protected fetch: Fetch;
|
|
6
|
-
constructor(url: string, { headers, customFetch, }: {
|
|
7
|
-
headers?: Record<string, string>;
|
|
8
|
-
customFetch?: Fetch;
|
|
9
|
-
});
|
|
10
|
-
/**
|
|
11
|
-
* Updates the authorization header
|
|
12
|
-
* @params token - the new jwt token sent in the authorisation header
|
|
13
|
-
*/
|
|
14
|
-
setAuth(token: string): void;
|
|
15
|
-
/**
|
|
16
|
-
* Invokes a function
|
|
17
|
-
* @param functionName - the name of the function to invoke
|
|
18
|
-
* @param invokeOptions - object with the following properties
|
|
19
|
-
* `headers`: object representing the headers to send with the request
|
|
20
|
-
* `body`: the body of the request
|
|
21
|
-
* `responseType`: how the response should be parsed. The default is `json`
|
|
22
|
-
*/
|
|
23
|
-
invoke<T = any>(functionName: string, invokeOptions?: FunctionInvokeOptions): Promise<{
|
|
24
|
-
data: T;
|
|
25
|
-
error: null;
|
|
26
|
-
} | {
|
|
27
|
-
data: null;
|
|
28
|
-
error: Error;
|
|
29
|
-
}>;
|
|
30
|
-
}
|
|
1
|
+
export { FunctionsClient } from './FunctionsClient';
|
|
2
|
+
export { FunctionsError, FunctionsFetchError, FunctionsHttpError, FunctionsRelayError, FunctionsResponse, } from './types';
|
|
31
3
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,kBAAkB,EAClB,mBAAmB,EACnB,iBAAiB,GAClB,MAAM,SAAS,CAAA"}
|
package/dist/module/index.js
CHANGED
|
@@ -1,67 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { resolveFetch } from './helper';
|
|
11
|
-
export class FunctionsClient {
|
|
12
|
-
constructor(url, { headers = {}, customFetch, }) {
|
|
13
|
-
this.url = url;
|
|
14
|
-
this.headers = headers;
|
|
15
|
-
this.fetch = resolveFetch(customFetch);
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Updates the authorization header
|
|
19
|
-
* @params token - the new jwt token sent in the authorisation header
|
|
20
|
-
*/
|
|
21
|
-
setAuth(token) {
|
|
22
|
-
this.headers.Authorization = `Bearer ${token}`;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Invokes a function
|
|
26
|
-
* @param functionName - the name of the function to invoke
|
|
27
|
-
* @param invokeOptions - object with the following properties
|
|
28
|
-
* `headers`: object representing the headers to send with the request
|
|
29
|
-
* `body`: the body of the request
|
|
30
|
-
* `responseType`: how the response should be parsed. The default is `json`
|
|
31
|
-
*/
|
|
32
|
-
invoke(functionName, invokeOptions) {
|
|
33
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
-
try {
|
|
35
|
-
const { headers, body } = invokeOptions !== null && invokeOptions !== void 0 ? invokeOptions : {};
|
|
36
|
-
const response = yield this.fetch(`${this.url}/${functionName}`, {
|
|
37
|
-
method: 'POST',
|
|
38
|
-
headers: Object.assign({}, this.headers, headers),
|
|
39
|
-
body,
|
|
40
|
-
});
|
|
41
|
-
const isRelayError = response.headers.get('x-relay-error');
|
|
42
|
-
if (isRelayError && isRelayError === 'true') {
|
|
43
|
-
return { data: null, error: new Error(yield response.text()) };
|
|
44
|
-
}
|
|
45
|
-
let data;
|
|
46
|
-
const { responseType } = invokeOptions !== null && invokeOptions !== void 0 ? invokeOptions : {};
|
|
47
|
-
if (!responseType || responseType === 'json') {
|
|
48
|
-
data = yield response.json();
|
|
49
|
-
}
|
|
50
|
-
else if (responseType === 'arrayBuffer') {
|
|
51
|
-
data = yield response.arrayBuffer();
|
|
52
|
-
}
|
|
53
|
-
else if (responseType === 'blob') {
|
|
54
|
-
data = yield response.blob();
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
data = yield response.text();
|
|
58
|
-
}
|
|
59
|
-
return { data, error: null };
|
|
60
|
-
}
|
|
61
|
-
catch (error) {
|
|
62
|
-
return { data: null, error };
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
}
|
|
1
|
+
export { FunctionsClient } from './FunctionsClient';
|
|
2
|
+
export { FunctionsError, FunctionsFetchError, FunctionsHttpError, FunctionsRelayError, } from './types';
|
|
67
3
|
//# sourceMappingURL=index.js.map
|
package/dist/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,kBAAkB,EAClB,mBAAmB,GAEpB,MAAM,SAAS,CAAA"}
|
package/dist/module/types.d.ts
CHANGED
|
@@ -1,15 +1,29 @@
|
|
|
1
1
|
export declare type Fetch = typeof fetch;
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Response format
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
interface FunctionsResponseSuccess {
|
|
7
|
+
data: any;
|
|
8
|
+
error: null;
|
|
9
|
+
}
|
|
10
|
+
interface FunctionsResponseFailure {
|
|
11
|
+
data: null;
|
|
12
|
+
error: any;
|
|
13
|
+
}
|
|
14
|
+
export declare type FunctionsResponse = FunctionsResponseSuccess | FunctionsResponseFailure;
|
|
15
|
+
export declare class FunctionsError extends Error {
|
|
16
|
+
context: any;
|
|
17
|
+
constructor(message: string, name?: string, context?: any);
|
|
18
|
+
}
|
|
19
|
+
export declare class FunctionsFetchError extends FunctionsError {
|
|
20
|
+
constructor(context: any);
|
|
21
|
+
}
|
|
22
|
+
export declare class FunctionsRelayError extends FunctionsError {
|
|
23
|
+
constructor(context: any);
|
|
24
|
+
}
|
|
25
|
+
export declare class FunctionsHttpError extends FunctionsError {
|
|
26
|
+
constructor(context: any);
|
|
27
|
+
}
|
|
28
|
+
export {};
|
|
15
29
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,KAAK,GAAG,OAAO,KAAK,CAAA;AAEhC,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,KAAK,GAAG,OAAO,KAAK,CAAA;AAEhC;;;GAGG;AACH,UAAU,wBAAwB;IAChC,IAAI,EAAE,GAAG,CAAA;IACT,KAAK,EAAE,IAAI,CAAA;CACZ;AACD,UAAU,wBAAwB;IAChC,IAAI,EAAE,IAAI,CAAA;IACV,KAAK,EAAE,GAAG,CAAA;CACX;AACD,oBAAY,iBAAiB,GAAG,wBAAwB,GAAG,wBAAwB,CAAA;AAEnF,qBAAa,cAAe,SAAQ,KAAK;IACvC,OAAO,EAAE,GAAG,CAAA;gBACA,OAAO,EAAE,MAAM,EAAE,IAAI,SAAmB,EAAE,OAAO,CAAC,EAAE,GAAG;CAKpE;AAED,qBAAa,mBAAoB,SAAQ,cAAc;gBACzC,OAAO,EAAE,GAAG;CAGzB;AAED,qBAAa,mBAAoB,SAAQ,cAAc;gBACzC,OAAO,EAAE,GAAG;CAGzB;AAED,qBAAa,kBAAmB,SAAQ,cAAc;gBACxC,OAAO,EAAE,GAAG;CAGzB"}
|
package/dist/module/types.js
CHANGED
|
@@ -1,8 +1,23 @@
|
|
|
1
|
-
export
|
|
2
|
-
(
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
1
|
+
export class FunctionsError extends Error {
|
|
2
|
+
constructor(message, name = 'FunctionsError', context) {
|
|
3
|
+
super(message);
|
|
4
|
+
super.name = name;
|
|
5
|
+
this.context = context;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export class FunctionsFetchError extends FunctionsError {
|
|
9
|
+
constructor(context) {
|
|
10
|
+
super('Failed to perform request to Edge Function', 'FunctionsFetchError', context);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class FunctionsRelayError extends FunctionsError {
|
|
14
|
+
constructor(context) {
|
|
15
|
+
super('Relay error communicating with deno backend', 'FunctionsRelayError', context);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export class FunctionsHttpError extends FunctionsError {
|
|
19
|
+
constructor(context) {
|
|
20
|
+
super('Edge Function returned a non-2xx status code', 'FunctionsHttpError', context);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
8
23
|
//# sourceMappingURL=types.js.map
|
package/dist/module/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAgBA,MAAM,OAAO,cAAe,SAAQ,KAAK;IAEvC,YAAY,OAAe,EAAE,IAAI,GAAG,gBAAgB,EAAE,OAAa;QACjE,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,KAAK,CAAC,IAAI,GAAG,IAAI,CAAA;QACjB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;CACF;AAED,MAAM,OAAO,mBAAoB,SAAQ,cAAc;IACrD,YAAY,OAAY;QACtB,KAAK,CAAC,4CAA4C,EAAE,qBAAqB,EAAE,OAAO,CAAC,CAAA;IACrF,CAAC;CACF;AAED,MAAM,OAAO,mBAAoB,SAAQ,cAAc;IACrD,YAAY,OAAY;QACtB,KAAK,CAAC,6CAA6C,EAAE,qBAAqB,EAAE,OAAO,CAAC,CAAA;IACtF,CAAC;CACF;AAED,MAAM,OAAO,kBAAmB,SAAQ,cAAc;IACpD,YAAY,OAAY;QACtB,KAAK,CAAC,8CAA8C,EAAE,oBAAoB,EAAE,OAAO,CAAC,CAAA;IACtF,CAAC;CACF"}
|
package/dist/module/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const version = "1.
|
|
1
|
+
export declare const version = "1.4.0-next.1";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/dist/module/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const version = '1.
|
|
1
|
+
export const version = '1.4.0-next.1';
|
|
2
2
|
//# sourceMappingURL=version.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supabase/functions-js",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0-next.1",
|
|
4
4
|
"description": "JS Client library to interact with Supabase Functions.",
|
|
5
5
|
"main": "dist/main/index.js",
|
|
6
6
|
"module": "dist/module/index.js",
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
"build": "run-s clean format build:*",
|
|
13
13
|
"build:main": "tsc -p tsconfig.json",
|
|
14
14
|
"build:module": "tsc -p tsconfig.module.json",
|
|
15
|
-
"docs": "typedoc src/index.ts",
|
|
16
|
-
"docs:json": "typedoc --json docs/spec.json --excludeExternals src/index.ts",
|
|
15
|
+
"docs": "typedoc src/index.ts src/types.ts --excludePrivate --excludeProtected",
|
|
16
|
+
"docs:json": "typedoc --json docs/spec.json --excludeExternals src/index.ts src/types.ts",
|
|
17
17
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"rimraf": "^3.0.2",
|
|
44
44
|
"semantic-release": "^19.0.2",
|
|
45
45
|
"semantic-release-plugin-update-version-in-files": "^1.1.0",
|
|
46
|
-
"typedoc": "^0.
|
|
46
|
+
"typedoc": "^0.23.10",
|
|
47
47
|
"typescript": "^4.6.2"
|
|
48
48
|
},
|
|
49
49
|
"publishConfig": {
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { resolveFetch } from './helper'
|
|
2
|
+
import {
|
|
3
|
+
Fetch,
|
|
4
|
+
FunctionsFetchError,
|
|
5
|
+
FunctionsHttpError,
|
|
6
|
+
FunctionsRelayError,
|
|
7
|
+
FunctionsResponse,
|
|
8
|
+
} from './types'
|
|
9
|
+
|
|
10
|
+
export class FunctionsClient {
|
|
11
|
+
protected url: string
|
|
12
|
+
protected headers: Record<string, string>
|
|
13
|
+
protected fetch: Fetch
|
|
14
|
+
|
|
15
|
+
constructor(
|
|
16
|
+
url: string,
|
|
17
|
+
{
|
|
18
|
+
headers = {},
|
|
19
|
+
customFetch,
|
|
20
|
+
}: {
|
|
21
|
+
headers?: Record<string, string>
|
|
22
|
+
customFetch?: Fetch
|
|
23
|
+
} = {}
|
|
24
|
+
) {
|
|
25
|
+
this.url = url
|
|
26
|
+
this.headers = headers
|
|
27
|
+
this.fetch = resolveFetch(customFetch)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Updates the authorization header
|
|
32
|
+
* @params token - the new jwt token sent in the authorisation header
|
|
33
|
+
*/
|
|
34
|
+
setAuth(token: string) {
|
|
35
|
+
this.headers.Authorization = `Bearer ${token}`
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Invokes a function
|
|
40
|
+
* @param functionName - the name of the function to invoke
|
|
41
|
+
* @param functionArgs - the arguments to the function
|
|
42
|
+
* @param options - function invoke options
|
|
43
|
+
* @param options.headers - headers to send with the request
|
|
44
|
+
*/
|
|
45
|
+
async invoke(
|
|
46
|
+
functionName: string,
|
|
47
|
+
functionArgs: any,
|
|
48
|
+
{
|
|
49
|
+
headers = {},
|
|
50
|
+
}: {
|
|
51
|
+
headers?: Record<string, string>
|
|
52
|
+
} = {}
|
|
53
|
+
): Promise<FunctionsResponse> {
|
|
54
|
+
try {
|
|
55
|
+
let _headers: Record<string, string> = {}
|
|
56
|
+
let body: any
|
|
57
|
+
if (functionArgs instanceof Blob || functionArgs instanceof ArrayBuffer) {
|
|
58
|
+
// will work for File as File inherits Blob
|
|
59
|
+
// also works for ArrayBuffer as it is the same underlying structure as a Blob
|
|
60
|
+
_headers['Content-Type'] = 'application/octet-stream'
|
|
61
|
+
body = functionArgs
|
|
62
|
+
} else if (typeof functionArgs === 'string') {
|
|
63
|
+
// plain string
|
|
64
|
+
_headers['Content-Type'] = 'text/plain'
|
|
65
|
+
body = functionArgs
|
|
66
|
+
} else if (functionArgs instanceof FormData) {
|
|
67
|
+
// don't set content-type headers
|
|
68
|
+
// Request will automatically add the right boundary value
|
|
69
|
+
body = functionArgs
|
|
70
|
+
} else {
|
|
71
|
+
// default, assume this is JSON
|
|
72
|
+
_headers['Content-Type'] = 'application/json'
|
|
73
|
+
body = JSON.stringify(functionArgs)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const response = await this.fetch(`${this.url}/${functionName}`, {
|
|
77
|
+
method: 'POST',
|
|
78
|
+
// headers priority is (high to low):
|
|
79
|
+
// 1. invoke-level headers
|
|
80
|
+
// 2. client-level headers
|
|
81
|
+
// 3. default Content-Type header
|
|
82
|
+
headers: { ..._headers, ...this.headers, ...headers },
|
|
83
|
+
body,
|
|
84
|
+
}).catch((fetchError) => {
|
|
85
|
+
throw new FunctionsFetchError(fetchError)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
const isRelayError = response.headers.get('x-relay-error')
|
|
89
|
+
if (isRelayError && isRelayError === 'true') {
|
|
90
|
+
throw new FunctionsRelayError(response)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (!response.ok) {
|
|
94
|
+
throw new FunctionsHttpError(response)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
let responseType = (response.headers.get('Content-Type') ?? 'text/plain').split(';')[0].trim()
|
|
98
|
+
let data: any
|
|
99
|
+
if (responseType === 'application/json') {
|
|
100
|
+
data = await response.json()
|
|
101
|
+
} else if (responseType === 'application/octet-stream') {
|
|
102
|
+
data = await response.blob()
|
|
103
|
+
} else if (responseType === 'multipart/form-data') {
|
|
104
|
+
data = await response.formData()
|
|
105
|
+
} else {
|
|
106
|
+
// default to text
|
|
107
|
+
data = await response.text()
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return { data, error: null }
|
|
111
|
+
} catch (error) {
|
|
112
|
+
return { data: null, error }
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
package/src/helper.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
type Fetch = typeof fetch
|
|
1
|
+
import { Fetch } from './types'
|
|
4
2
|
|
|
5
3
|
export const resolveFetch = (customFetch?: Fetch): Fetch => {
|
|
6
4
|
let _fetch: Fetch
|
|
7
5
|
if (customFetch) {
|
|
8
6
|
_fetch = customFetch
|
|
9
7
|
} else if (typeof fetch === 'undefined') {
|
|
10
|
-
_fetch =
|
|
8
|
+
_fetch = async (...args) => await (await import('cross-fetch')).fetch(...args)
|
|
11
9
|
} else {
|
|
12
10
|
_fetch = fetch
|
|
13
11
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,74 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
constructor(
|
|
10
|
-
url: string,
|
|
11
|
-
{
|
|
12
|
-
headers = {},
|
|
13
|
-
customFetch,
|
|
14
|
-
}: {
|
|
15
|
-
headers?: Record<string, string>
|
|
16
|
-
customFetch?: Fetch
|
|
17
|
-
}
|
|
18
|
-
) {
|
|
19
|
-
this.url = url
|
|
20
|
-
this.headers = headers
|
|
21
|
-
this.fetch = resolveFetch(customFetch)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Updates the authorization header
|
|
26
|
-
* @params token - the new jwt token sent in the authorisation header
|
|
27
|
-
*/
|
|
28
|
-
setAuth(token: string) {
|
|
29
|
-
this.headers.Authorization = `Bearer ${token}`
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Invokes a function
|
|
34
|
-
* @param functionName - the name of the function to invoke
|
|
35
|
-
* @param invokeOptions - object with the following properties
|
|
36
|
-
* `headers`: object representing the headers to send with the request
|
|
37
|
-
* `body`: the body of the request
|
|
38
|
-
* `responseType`: how the response should be parsed. The default is `json`
|
|
39
|
-
*/
|
|
40
|
-
async invoke<T = any>(
|
|
41
|
-
functionName: string,
|
|
42
|
-
invokeOptions?: FunctionInvokeOptions
|
|
43
|
-
): Promise<{ data: T; error: null } | { data: null; error: Error }> {
|
|
44
|
-
try {
|
|
45
|
-
const { headers, body } = invokeOptions ?? {}
|
|
46
|
-
const response = await this.fetch(`${this.url}/${functionName}`, {
|
|
47
|
-
method: 'POST',
|
|
48
|
-
headers: Object.assign({}, this.headers, headers),
|
|
49
|
-
body,
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
const isRelayError = response.headers.get('x-relay-error')
|
|
53
|
-
if (isRelayError && isRelayError === 'true') {
|
|
54
|
-
return { data: null, error: new Error(await response.text()) }
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
let data
|
|
58
|
-
const { responseType } = invokeOptions ?? {}
|
|
59
|
-
if (!responseType || responseType === 'json') {
|
|
60
|
-
data = await response.json()
|
|
61
|
-
} else if (responseType === 'arrayBuffer') {
|
|
62
|
-
data = await response.arrayBuffer()
|
|
63
|
-
} else if (responseType === 'blob') {
|
|
64
|
-
data = await response.blob()
|
|
65
|
-
} else {
|
|
66
|
-
data = await response.text()
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return { data, error: null }
|
|
70
|
-
} catch (error: any) {
|
|
71
|
-
return { data: null, error }
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
1
|
+
export { FunctionsClient } from './FunctionsClient'
|
|
2
|
+
export {
|
|
3
|
+
FunctionsError,
|
|
4
|
+
FunctionsFetchError,
|
|
5
|
+
FunctionsHttpError,
|
|
6
|
+
FunctionsRelayError,
|
|
7
|
+
FunctionsResponse,
|
|
8
|
+
} from './types'
|
package/src/types.ts
CHANGED
|
@@ -1,14 +1,42 @@
|
|
|
1
1
|
export type Fetch = typeof fetch
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Response format
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
interface FunctionsResponseSuccess {
|
|
8
|
+
data: any
|
|
9
|
+
error: null
|
|
10
|
+
}
|
|
11
|
+
interface FunctionsResponseFailure {
|
|
12
|
+
data: null
|
|
13
|
+
error: any
|
|
14
|
+
}
|
|
15
|
+
export type FunctionsResponse = FunctionsResponseSuccess | FunctionsResponseFailure
|
|
16
|
+
|
|
17
|
+
export class FunctionsError extends Error {
|
|
18
|
+
context: any
|
|
19
|
+
constructor(message: string, name = 'FunctionsError', context?: any) {
|
|
20
|
+
super(message)
|
|
21
|
+
super.name = name
|
|
22
|
+
this.context = context
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class FunctionsFetchError extends FunctionsError {
|
|
27
|
+
constructor(context: any) {
|
|
28
|
+
super('Failed to perform request to Edge Function', 'FunctionsFetchError', context)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class FunctionsRelayError extends FunctionsError {
|
|
33
|
+
constructor(context: any) {
|
|
34
|
+
super('Relay error communicating with deno backend', 'FunctionsRelayError', context)
|
|
35
|
+
}
|
|
8
36
|
}
|
|
9
37
|
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
38
|
+
export class FunctionsHttpError extends FunctionsError {
|
|
39
|
+
constructor(context: any) {
|
|
40
|
+
super('Edge Function returned a non-2xx status code', 'FunctionsHttpError', context)
|
|
41
|
+
}
|
|
14
42
|
}
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '1.
|
|
1
|
+
export const version = '1.4.0-next.1'
|