@supabase/functions-js 1.3.4 → 1.4.0-next.3

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.
Files changed (39) hide show
  1. package/README.md +3 -0
  2. package/dist/main/FunctionsClient.d.ts +26 -0
  3. package/dist/main/FunctionsClient.d.ts.map +1 -0
  4. package/dist/main/FunctionsClient.js +107 -0
  5. package/dist/main/FunctionsClient.js.map +1 -0
  6. package/dist/main/helper.d.ts +1 -2
  7. package/dist/main/helper.d.ts.map +1 -1
  8. package/dist/main/index.d.ts +2 -30
  9. package/dist/main/index.d.ts.map +1 -1
  10. package/dist/main/index.js +8 -68
  11. package/dist/main/index.js.map +1 -1
  12. package/dist/main/types.d.ts +27 -13
  13. package/dist/main/types.d.ts.map +1 -1
  14. package/dist/main/types.js +27 -8
  15. package/dist/main/types.js.map +1 -1
  16. package/dist/main/version.d.ts +1 -1
  17. package/dist/main/version.js +1 -1
  18. package/dist/module/FunctionsClient.d.ts +26 -0
  19. package/dist/module/FunctionsClient.d.ts.map +1 -0
  20. package/dist/module/FunctionsClient.js +103 -0
  21. package/dist/module/FunctionsClient.js.map +1 -0
  22. package/dist/module/helper.d.ts +1 -2
  23. package/dist/module/helper.d.ts.map +1 -1
  24. package/dist/module/index.d.ts +2 -30
  25. package/dist/module/index.d.ts.map +1 -1
  26. package/dist/module/index.js +2 -66
  27. package/dist/module/index.js.map +1 -1
  28. package/dist/module/types.d.ts +27 -13
  29. package/dist/module/types.d.ts.map +1 -1
  30. package/dist/module/types.js +22 -7
  31. package/dist/module/types.js.map +1 -1
  32. package/dist/module/version.d.ts +1 -1
  33. package/dist/module/version.js +1 -1
  34. package/package.json +4 -4
  35. package/src/FunctionsClient.ts +120 -0
  36. package/src/helper.ts +1 -1
  37. package/src/index.ts +8 -74
  38. package/src/types.ts +37 -9
  39. package/src/version.ts +1 -1
package/README.md CHANGED
@@ -1,3 +1,6 @@
1
1
  # `functions-js`
2
2
 
3
3
  JS Client library to interact with Supabase Functions.
4
+
5
+ ## Docs
6
+ https://supabase.github.io/functions-js/index.html
@@ -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;CAmE9B"}
@@ -0,0 +1,107 @@
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 && !Object.prototype.hasOwnProperty.call(headers, 'Content-Type')) {
42
+ if ((typeof Blob !== 'undefined' && functionArgs instanceof Blob) ||
43
+ functionArgs instanceof ArrayBuffer) {
44
+ // will work for File as File inherits Blob
45
+ // also works for ArrayBuffer as it is the same underlying structure as a Blob
46
+ _headers['Content-Type'] = 'application/octet-stream';
47
+ body = functionArgs;
48
+ }
49
+ else if (typeof functionArgs === 'string') {
50
+ // plain string
51
+ _headers['Content-Type'] = 'text/plain';
52
+ body = functionArgs;
53
+ }
54
+ else if (typeof FormData !== 'undefined' && functionArgs instanceof FormData) {
55
+ // don't set content-type headers
56
+ // Request will automatically add the right boundary value
57
+ body = functionArgs;
58
+ }
59
+ else {
60
+ // default, assume this is JSON
61
+ _headers['Content-Type'] = 'application/json';
62
+ body = JSON.stringify(functionArgs);
63
+ }
64
+ }
65
+ const response = yield this.fetch(`${this.url}/${functionName}`, {
66
+ method: 'POST',
67
+ // headers priority is (high to low):
68
+ // 1. invoke-level headers
69
+ // 2. client-level headers
70
+ // 3. default Content-Type header
71
+ headers: Object.assign(Object.assign(Object.assign({}, _headers), this.headers), headers),
72
+ body,
73
+ }).catch((fetchError) => {
74
+ throw new types_1.FunctionsFetchError(fetchError);
75
+ });
76
+ const isRelayError = response.headers.get('x-relay-error');
77
+ if (isRelayError && isRelayError === 'true') {
78
+ throw new types_1.FunctionsRelayError(response);
79
+ }
80
+ if (!response.ok) {
81
+ throw new types_1.FunctionsHttpError(response);
82
+ }
83
+ let responseType = ((_a = response.headers.get('Content-Type')) !== null && _a !== void 0 ? _a : 'text/plain').split(';')[0].trim();
84
+ let data;
85
+ if (responseType === 'application/json') {
86
+ data = yield response.json();
87
+ }
88
+ else if (responseType === 'application/octet-stream') {
89
+ data = yield response.blob();
90
+ }
91
+ else if (responseType === 'multipart/form-data') {
92
+ data = yield response.formData();
93
+ }
94
+ else {
95
+ // default to text
96
+ data = yield response.text();
97
+ }
98
+ return { data, error: null };
99
+ }
100
+ catch (error) {
101
+ return { data: null, error };
102
+ }
103
+ });
104
+ }
105
+ }
106
+ exports.FunctionsClient = FunctionsClient;
107
+ //# sourceMappingURL=FunctionsClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"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B,EAAE;oBACtD,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAA;iBAC7B;qBAAM,IAAI,YAAY,KAAK,qBAAqB,EAAE;oBACjD,IAAI,GAAG,MAAM,QAAQ,CAAC,QAAQ,EAAE,CAAA;iBACjC;qBAAM;oBACL,kBAAkB;oBAClB,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAA;iBAC7B;gBAED,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAA;aAC7B;YAAC,OAAO,KAAK,EAAE;gBACd,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;aAC7B;;KACF;CACF;AA9GD,0CA8GC"}
@@ -1,4 +1,3 @@
1
- declare type Fetch = typeof fetch;
1
+ import { Fetch } from './types';
2
2
  export declare const resolveFetch: (customFetch?: typeof fetch | undefined) => Fetch;
3
- export {};
4
3
  //# sourceMappingURL=helper.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../../src/helper.ts"],"names":[],"mappings":"AAAA,aAAK,KAAK,GAAG,OAAO,KAAK,CAAA;AAEzB,eAAO,MAAM,YAAY,8CAA0B,KAUlD,CAAA"}
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"}
@@ -1,31 +1,3 @@
1
- import { Fetch, FunctionInvokeOptions } 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 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":"AACA,OAAO,EAAE,KAAK,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAA;AAEtD,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;;;;;;;OAOG;IACG,MAAM,CAAC,CAAC,GAAG,GAAG,EAClB,YAAY,EAAE,MAAM,EACpB,aAAa,CAAC,EAAE,qBAAqB,GACpC,OAAO,CAAC;QAAE,IAAI,EAAE,CAAC,CAAC;QAAC,KAAK,EAAE,IAAI,CAAA;KAAE,GAAG;QAAE,IAAI,EAAE,IAAI,CAAC;QAAC,KAAK,EAAE,KAAK,CAAA;KAAE,CAAC;CA+BpE"}
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"}
@@ -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
- const helper_1 = require("./helper");
14
- class FunctionsClient {
15
- constructor(url, { headers = {}, customFetch, } = {}) {
16
- this.url = url;
17
- this.headers = headers;
18
- this.fetch = (0, helper_1.resolveFetch)(customFetch);
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
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qCAAuC;AAGvC,MAAa,eAAe;IAK1B,YACE,GAAW,EACX,EACE,OAAO,GAAG,EAAE,EACZ,WAAW,MAIT,EAAE;QAEN,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;QACd,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,KAAK,GAAG,IAAA,qBAAY,EAAC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C,IAAI,EAAE,CAAA;iBAC7B;qBAAM;oBACL,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAA;iBAC7B;gBAED,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAA;aAC7B;YAAC,OAAO,KAAU,EAAE;gBACnB,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;aAC7B;QACH,CAAC;KAAA;CACF;AAtED,0CAsEC"}
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"}
@@ -1,15 +1,29 @@
1
1
  export declare type Fetch = typeof fetch;
2
- export declare enum ResponseType {
3
- json = 0,
4
- text = 1,
5
- arrayBuffer = 2,
6
- blob = 3
7
- }
8
- export declare type FunctionInvokeOptions = {
9
- headers?: {
10
- [key: string]: string;
11
- };
12
- body?: Blob | BufferSource | FormData | URLSearchParams | ReadableStream<Uint8Array> | string;
13
- responseType?: keyof typeof ResponseType;
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,oBAAY,YAAY;IACtB,IAAI,IAAA;IACJ,IAAI,IAAA;IACJ,WAAW,IAAA;IACX,IAAI,IAAA;CACL;AAED,oBAAY,qBAAqB,GAAG;IAClC,OAAO,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAA;IACnC,IAAI,CAAC,EAAE,IAAI,GAAG,YAAY,GAAG,QAAQ,GAAG,eAAe,GAAG,cAAc,CAAC,UAAU,CAAC,GAAG,MAAM,CAAA;IAC7F,YAAY,CAAC,EAAE,MAAM,OAAO,YAAY,CAAA;CACzC,CAAA"}
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"}
@@ -1,11 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ResponseType = void 0;
4
- var ResponseType;
5
- (function (ResponseType) {
6
- ResponseType[ResponseType["json"] = 0] = "json";
7
- ResponseType[ResponseType["text"] = 1] = "text";
8
- ResponseType[ResponseType["arrayBuffer"] = 2] = "arrayBuffer";
9
- ResponseType[ResponseType["blob"] = 3] = "blob";
10
- })(ResponseType = exports.ResponseType || (exports.ResponseType = {}));
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
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":";;;AAEA,IAAY,YAKX;AALD,WAAY,YAAY;IACtB,+CAAI,CAAA;IACJ,+CAAI,CAAA;IACJ,6DAAW,CAAA;IACX,+CAAI,CAAA;AACN,CAAC,EALW,YAAY,GAAZ,oBAAY,KAAZ,oBAAY,QAKvB"}
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"}
@@ -1,2 +1,2 @@
1
- export declare const version = "1.3.4";
1
+ export declare const version = "1.4.0-next.3";
2
2
  //# sourceMappingURL=version.d.ts.map
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.version = void 0;
4
- exports.version = '1.3.4';
4
+ exports.version = '1.4.0-next.3';
5
5
  //# sourceMappingURL=version.js.map
@@ -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;CAmE9B"}
@@ -0,0 +1,103 @@
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 && !Object.prototype.hasOwnProperty.call(headers, 'Content-Type')) {
39
+ if ((typeof Blob !== 'undefined' && functionArgs instanceof Blob) ||
40
+ functionArgs instanceof ArrayBuffer) {
41
+ // will work for File as File inherits Blob
42
+ // also works for ArrayBuffer as it is the same underlying structure as a Blob
43
+ _headers['Content-Type'] = 'application/octet-stream';
44
+ body = functionArgs;
45
+ }
46
+ else if (typeof functionArgs === 'string') {
47
+ // plain string
48
+ _headers['Content-Type'] = 'text/plain';
49
+ body = functionArgs;
50
+ }
51
+ else if (typeof FormData !== 'undefined' && functionArgs instanceof FormData) {
52
+ // don't set content-type headers
53
+ // Request will automatically add the right boundary value
54
+ body = functionArgs;
55
+ }
56
+ else {
57
+ // default, assume this is JSON
58
+ _headers['Content-Type'] = 'application/json';
59
+ body = JSON.stringify(functionArgs);
60
+ }
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 FunctionsFetchError(fetchError);
72
+ });
73
+ const isRelayError = response.headers.get('x-relay-error');
74
+ if (isRelayError && isRelayError === 'true') {
75
+ throw new FunctionsRelayError(response);
76
+ }
77
+ if (!response.ok) {
78
+ throw new 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
+ //# sourceMappingURL=FunctionsClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FunctionsClient.js","sourceRoot":"","sources":["../../src/FunctionsClient.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AACvC,OAAO,EAEL,mBAAmB,EACnB,kBAAkB,EAClB,mBAAmB,GAEpB,MAAM,SAAS,CAAA;AAEhB,MAAM,OAAO,eAAe;IAK1B,YACE,GAAW,EACX,EACE,OAAO,GAAG,EAAE,EACZ,WAAW,MAIT,EAAE;QAEN,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;QACd,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,KAAK,GAAG,YAAY,CAAC,WAAW,CAAC,CAAA;IACxC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EAAE;gBACd,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;aAC7B;;KACF;CACF"}
@@ -1,4 +1,3 @@
1
- declare type Fetch = typeof fetch;
1
+ import { Fetch } from './types';
2
2
  export declare const resolveFetch: (customFetch?: typeof fetch | undefined) => Fetch;
3
- export {};
4
3
  //# sourceMappingURL=helper.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../../src/helper.ts"],"names":[],"mappings":"AAAA,aAAK,KAAK,GAAG,OAAO,KAAK,CAAA;AAEzB,eAAO,MAAM,YAAY,8CAA0B,KAUlD,CAAA"}
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"}
@@ -1,31 +1,3 @@
1
- import { Fetch, FunctionInvokeOptions } 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 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":"AACA,OAAO,EAAE,KAAK,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAA;AAEtD,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;;;;;;;OAOG;IACG,MAAM,CAAC,CAAC,GAAG,GAAG,EAClB,YAAY,EAAE,MAAM,EACpB,aAAa,CAAC,EAAE,qBAAqB,GACpC,OAAO,CAAC;QAAE,IAAI,EAAE,CAAC,CAAC;QAAC,KAAK,EAAE,IAAI,CAAA;KAAE,GAAG;QAAE,IAAI,EAAE,IAAI,CAAC;QAAC,KAAK,EAAE,KAAK,CAAA;KAAE,CAAC;CA+BpE"}
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"}
@@ -1,67 +1,3 @@
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
- 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
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AAGvC,MAAM,OAAO,eAAe;IAK1B,YACE,GAAW,EACX,EACE,OAAO,GAAG,EAAE,EACZ,WAAW,MAIT,EAAE;QAEN,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;QACd,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,KAAK,GAAG,YAAY,CAAC,WAAW,CAAC,CAAA;IACxC,CAAC;IAED;;;OAGG;IACH,OAAO,CAAC,KAAa;QACnB,IAAI,CAAC,OAAO,CAAC,aAAa,GAAG,UAAU,KAAK,EAAE,CAAA;IAChD,CAAC;IAED;;;;;;;OAOG;IACG,MAAM,CACV,YAAoB,EACpB,aAAqC;;YAErC,IAAI;gBACF,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,aAAa,aAAb,aAAa,cAAb,aAAa,GAAI,EAAE,CAAA;gBAC7C,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,YAAY,EAAE,EAAE;oBAC/D,MAAM,EAAE,MAAM;oBACd,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC;oBACjD,IAAI;iBACL,CAAC,CAAA;gBAEF,MAAM,YAAY,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAA;gBAC1D,IAAI,YAAY,IAAI,YAAY,KAAK,MAAM,EAAE;oBAC3C,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,KAAK,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,EAAE,CAAA;iBAC/D;gBAED,IAAI,IAAI,CAAA;gBACR,MAAM,EAAE,YAAY,EAAE,GAAG,aAAa,aAAb,aAAa,cAAb,aAAa,GAAI,EAAE,CAAA;gBAC5C,IAAI,CAAC,YAAY,IAAI,YAAY,KAAK,MAAM,EAAE;oBAC5C,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAA;iBAC7B;qBAAM,IAAI,YAAY,KAAK,aAAa,EAAE;oBACzC,IAAI,GAAG,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAA;iBACpC;qBAAM,IAAI,YAAY,KAAK,MAAM,EAAE;oBAClC,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAA;iBAC7B;qBAAM;oBACL,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAA;iBAC7B;gBAED,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAA;aAC7B;YAAC,OAAO,KAAU,EAAE;gBACnB,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;aAC7B;QACH,CAAC;KAAA;CACF"}
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"}
@@ -1,15 +1,29 @@
1
1
  export declare type Fetch = typeof fetch;
2
- export declare enum ResponseType {
3
- json = 0,
4
- text = 1,
5
- arrayBuffer = 2,
6
- blob = 3
7
- }
8
- export declare type FunctionInvokeOptions = {
9
- headers?: {
10
- [key: string]: string;
11
- };
12
- body?: Blob | BufferSource | FormData | URLSearchParams | ReadableStream<Uint8Array> | string;
13
- responseType?: keyof typeof ResponseType;
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,oBAAY,YAAY;IACtB,IAAI,IAAA;IACJ,IAAI,IAAA;IACJ,WAAW,IAAA;IACX,IAAI,IAAA;CACL;AAED,oBAAY,qBAAqB,GAAG;IAClC,OAAO,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAA;IACnC,IAAI,CAAC,EAAE,IAAI,GAAG,YAAY,GAAG,QAAQ,GAAG,eAAe,GAAG,cAAc,CAAC,UAAU,CAAC,GAAG,MAAM,CAAA;IAC7F,YAAY,CAAC,EAAE,MAAM,OAAO,YAAY,CAAA;CACzC,CAAA"}
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"}
@@ -1,8 +1,23 @@
1
- export var ResponseType;
2
- (function (ResponseType) {
3
- ResponseType[ResponseType["json"] = 0] = "json";
4
- ResponseType[ResponseType["text"] = 1] = "text";
5
- ResponseType[ResponseType["arrayBuffer"] = 2] = "arrayBuffer";
6
- ResponseType[ResponseType["blob"] = 3] = "blob";
7
- })(ResponseType || (ResponseType = {}));
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
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAEA,MAAM,CAAN,IAAY,YAKX;AALD,WAAY,YAAY;IACtB,+CAAI,CAAA;IACJ,+CAAI,CAAA;IACJ,6DAAW,CAAA;IACX,+CAAI,CAAA;AACN,CAAC,EALW,YAAY,KAAZ,YAAY,QAKvB"}
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"}
@@ -1,2 +1,2 @@
1
- export declare const version = "1.3.4";
1
+ export declare const version = "1.4.0-next.3";
2
2
  //# sourceMappingURL=version.d.ts.map
@@ -1,2 +1,2 @@
1
- export const version = '1.3.4';
1
+ export const version = '1.4.0-next.3';
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.4",
3
+ "version": "1.4.0-next.3",
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.22.13",
46
+ "typedoc": "^0.23.10",
47
47
  "typescript": "^4.6.2"
48
48
  },
49
49
  "publishConfig": {
@@ -0,0 +1,120 @@
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 && !Object.prototype.hasOwnProperty.call(headers, 'Content-Type')) {
58
+ if (
59
+ (typeof Blob !== 'undefined' && functionArgs instanceof Blob) ||
60
+ functionArgs instanceof ArrayBuffer
61
+ ) {
62
+ // will work for File as File inherits Blob
63
+ // also works for ArrayBuffer as it is the same underlying structure as a Blob
64
+ _headers['Content-Type'] = 'application/octet-stream'
65
+ body = functionArgs
66
+ } else if (typeof functionArgs === 'string') {
67
+ // plain string
68
+ _headers['Content-Type'] = 'text/plain'
69
+ body = functionArgs
70
+ } else if (typeof FormData !== 'undefined' && functionArgs instanceof FormData) {
71
+ // don't set content-type headers
72
+ // Request will automatically add the right boundary value
73
+ body = functionArgs
74
+ } else {
75
+ // default, assume this is JSON
76
+ _headers['Content-Type'] = 'application/json'
77
+ body = JSON.stringify(functionArgs)
78
+ }
79
+ }
80
+
81
+ const response = await this.fetch(`${this.url}/${functionName}`, {
82
+ method: 'POST',
83
+ // headers priority is (high to low):
84
+ // 1. invoke-level headers
85
+ // 2. client-level headers
86
+ // 3. default Content-Type header
87
+ headers: { ..._headers, ...this.headers, ...headers },
88
+ body,
89
+ }).catch((fetchError) => {
90
+ throw new FunctionsFetchError(fetchError)
91
+ })
92
+
93
+ const isRelayError = response.headers.get('x-relay-error')
94
+ if (isRelayError && isRelayError === 'true') {
95
+ throw new FunctionsRelayError(response)
96
+ }
97
+
98
+ if (!response.ok) {
99
+ throw new FunctionsHttpError(response)
100
+ }
101
+
102
+ let responseType = (response.headers.get('Content-Type') ?? 'text/plain').split(';')[0].trim()
103
+ let data: any
104
+ if (responseType === 'application/json') {
105
+ data = await response.json()
106
+ } else if (responseType === 'application/octet-stream') {
107
+ data = await response.blob()
108
+ } else if (responseType === 'multipart/form-data') {
109
+ data = await response.formData()
110
+ } else {
111
+ // default to text
112
+ data = await response.text()
113
+ }
114
+
115
+ return { data, error: null }
116
+ } catch (error) {
117
+ return { data: null, error }
118
+ }
119
+ }
120
+ }
package/src/helper.ts CHANGED
@@ -1,4 +1,4 @@
1
- type Fetch = typeof fetch
1
+ import { Fetch } from './types'
2
2
 
3
3
  export const resolveFetch = (customFetch?: Fetch): Fetch => {
4
4
  let _fetch: Fetch
package/src/index.ts CHANGED
@@ -1,74 +1,8 @@
1
- import { resolveFetch } from './helper'
2
- import { Fetch, FunctionInvokeOptions } from './types'
3
-
4
- export class FunctionsClient {
5
- protected url: string
6
- protected headers: Record<string, string>
7
- protected fetch: Fetch
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
- export enum ResponseType {
4
- json,
5
- text,
6
- arrayBuffer,
7
- blob,
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 type FunctionInvokeOptions = {
11
- headers?: { [key: string]: string }
12
- body?: Blob | BufferSource | FormData | URLSearchParams | ReadableStream<Uint8Array> | string
13
- responseType?: keyof typeof ResponseType
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.3.4'
1
+ export const version = '1.4.0-next.3'