@plasmicpkgs/fetch 0.0.2

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Plasmic
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,20 @@
1
+ import registerFunction from '@plasmicapp/host/registerFunction';
2
+
3
+ declare function fetch_2(url: string, method: HTTPMethod, headers: Record<string, string>, body?: string | object): Promise<{
4
+ statusCode: number;
5
+ headers: {
6
+ [k: string]: string;
7
+ };
8
+ response: any;
9
+ }>;
10
+ export { fetch_2 as fetch }
11
+
12
+ declare type HTTPMethod = "GET" | "POST" | "PUT" | "DELETE";
13
+
14
+ declare type Registerable = {
15
+ registerFunction: typeof registerFunction;
16
+ };
17
+
18
+ export declare function registerFetch(loader?: Registerable): void;
19
+
20
+ export { }
@@ -0,0 +1,103 @@
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+
22
+ // src/index.ts
23
+ import registerFunction from "@plasmicapp/host/registerFunction";
24
+ function base64StringToBuffer(bstr) {
25
+ try {
26
+ bstr = atob(bstr);
27
+ } catch (e) {
28
+ throw new Error("Invalid base64 for binary type");
29
+ }
30
+ const uint8Array = new Uint8Array(bstr.length);
31
+ for (let i = 0; i < bstr.length; i++) {
32
+ uint8Array[i] = bstr.charCodeAt(i);
33
+ }
34
+ return uint8Array.buffer;
35
+ }
36
+ function bodyToFetchBody(body) {
37
+ if (body == null) {
38
+ return void 0;
39
+ } else if (typeof body === "object") {
40
+ return JSON.stringify(body);
41
+ } else if (body.startsWith("@binary")) {
42
+ return base64StringToBuffer(body.slice("@binary".length));
43
+ } else {
44
+ return body;
45
+ }
46
+ }
47
+ function maybeParseJSON(json) {
48
+ try {
49
+ return JSON.parse(json);
50
+ } catch (e) {
51
+ return json;
52
+ }
53
+ }
54
+ function wrappedFetch(url, method, headers, body) {
55
+ return __async(this, null, function* () {
56
+ const response = yield fetch(url, {
57
+ method,
58
+ headers,
59
+ body: bodyToFetchBody(body)
60
+ });
61
+ const statusCode = response.status;
62
+ const responseText = yield response.text();
63
+ return {
64
+ statusCode,
65
+ headers: Object.fromEntries(response.headers.entries()),
66
+ response: maybeParseJSON(responseText)
67
+ };
68
+ });
69
+ }
70
+ var registerFetchParams = {
71
+ name: "fetch",
72
+ importPath: "@plasmicapp/fetch",
73
+ params: [
74
+ {
75
+ name: "url",
76
+ type: "string"
77
+ },
78
+ {
79
+ name: "method",
80
+ type: "string"
81
+ },
82
+ {
83
+ name: "headers",
84
+ type: "object"
85
+ },
86
+ {
87
+ name: "body",
88
+ type: "object"
89
+ }
90
+ ]
91
+ };
92
+ function registerFetch(loader) {
93
+ if (loader) {
94
+ loader.registerFunction(wrappedFetch, registerFetchParams);
95
+ } else {
96
+ registerFunction(wrappedFetch, registerFetchParams);
97
+ }
98
+ }
99
+ export {
100
+ wrappedFetch as fetch,
101
+ registerFetch
102
+ };
103
+ //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts"],
4
+ "sourcesContent": ["import registerFunction, {\n CustomFunctionMeta,\n} from \"@plasmicapp/host/registerFunction\";\n\ntype Registerable = {\n registerFunction: typeof registerFunction;\n};\n\n// Some functions were extracted from platform/wab/src/wab/server/data-sources/http-fetcher.ts\n\ntype HTTPMethod = \"GET\" | \"POST\" | \"PUT\" | \"DELETE\";\n\nfunction base64StringToBuffer(bstr: string) {\n try {\n bstr = atob(bstr);\n } catch (e) {\n throw new Error(\"Invalid base64 for binary type\");\n }\n const uint8Array = new Uint8Array(bstr.length);\n for (let i = 0; i < bstr.length; i++) {\n uint8Array[i] = bstr.charCodeAt(i);\n }\n return uint8Array.buffer;\n}\n\nfunction bodyToFetchBody(body?: string | object) {\n if (body == null) {\n return undefined;\n } else if (typeof body === \"object\") {\n return JSON.stringify(body);\n } else if (body.startsWith(\"@binary\")) {\n return base64StringToBuffer(body.slice(\"@binary\".length));\n } else {\n return body;\n }\n}\n\nfunction maybeParseJSON(json: string) {\n try {\n return JSON.parse(json);\n } catch (e) {\n return json;\n }\n}\n\nasync function wrappedFetch(\n url: string,\n method: HTTPMethod,\n headers: Record<string, string>,\n body?: string | object\n) {\n const response = await fetch(url, {\n method,\n headers,\n body: bodyToFetchBody(body),\n });\n\n const statusCode = response.status;\n const responseText = await response.text();\n\n return {\n statusCode,\n headers: Object.fromEntries(response.headers.entries()),\n response: maybeParseJSON(responseText),\n };\n}\n\nexport { wrappedFetch as fetch };\n\nconst registerFetchParams: CustomFunctionMeta<typeof wrappedFetch> = {\n name: \"fetch\",\n importPath: \"@plasmicapp/fetch\",\n params: [\n {\n name: \"url\",\n type: \"string\",\n },\n {\n name: \"method\",\n type: \"string\",\n },\n {\n name: \"headers\",\n type: \"object\",\n },\n {\n name: \"body\",\n type: \"object\",\n },\n ],\n};\n\nexport function registerFetch(loader?: Registerable) {\n if (loader) {\n loader.registerFunction(wrappedFetch, registerFetchParams);\n } else {\n registerFunction(wrappedFetch, registerFetchParams);\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,sBAEA;AAUP,SAAS,qBAAqB,MAAc;AAC1C,MAAI;AACF,WAAO,KAAK,IAAI;AAAA,EAClB,SAAS,GAAP;AACA,UAAM,IAAI,MAAM,gCAAgC;AAAA,EAClD;AACA,QAAM,aAAa,IAAI,WAAW,KAAK,MAAM;AAC7C,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,eAAW,CAAC,IAAI,KAAK,WAAW,CAAC;AAAA,EACnC;AACA,SAAO,WAAW;AACpB;AAEA,SAAS,gBAAgB,MAAwB;AAC/C,MAAI,QAAQ,MAAM;AAChB,WAAO;AAAA,EACT,WAAW,OAAO,SAAS,UAAU;AACnC,WAAO,KAAK,UAAU,IAAI;AAAA,EAC5B,WAAW,KAAK,WAAW,SAAS,GAAG;AACrC,WAAO,qBAAqB,KAAK,MAAM,UAAU,MAAM,CAAC;AAAA,EAC1D,OAAO;AACL,WAAO;AAAA,EACT;AACF;AAEA,SAAS,eAAe,MAAc;AACpC,MAAI;AACF,WAAO,KAAK,MAAM,IAAI;AAAA,EACxB,SAAS,GAAP;AACA,WAAO;AAAA,EACT;AACF;AAEA,SAAe,aACb,KACA,QACA,SACA,MACA;AAAA;AACA,UAAM,WAAW,MAAM,MAAM,KAAK;AAAA,MAChC;AAAA,MACA;AAAA,MACA,MAAM,gBAAgB,IAAI;AAAA,IAC5B,CAAC;AAED,UAAM,aAAa,SAAS;AAC5B,UAAM,eAAe,MAAM,SAAS,KAAK;AAEzC,WAAO;AAAA,MACL;AAAA,MACA,SAAS,OAAO,YAAY,SAAS,QAAQ,QAAQ,CAAC;AAAA,MACtD,UAAU,eAAe,YAAY;AAAA,IACvC;AAAA,EACF;AAAA;AAIA,IAAM,sBAA+D;AAAA,EACnE,MAAM;AAAA,EACN,YAAY;AAAA,EACZ,QAAQ;AAAA,IACN;AAAA,MACE,MAAM;AAAA,MACN,MAAM;AAAA,IACR;AAAA,IACA;AAAA,MACE,MAAM;AAAA,MACN,MAAM;AAAA,IACR;AAAA,IACA;AAAA,MACE,MAAM;AAAA,MACN,MAAM;AAAA,IACR;AAAA,IACA;AAAA,MACE,MAAM;AAAA,MACN,MAAM;AAAA,IACR;AAAA,EACF;AACF;AAEO,SAAS,cAAc,QAAuB;AACnD,MAAI,QAAQ;AACV,WAAO,iBAAiB,cAAc,mBAAmB;AAAA,EAC3D,OAAO;AACL,qBAAiB,cAAc,mBAAmB;AAAA,EACpD;AACF;",
6
+ "names": []
7
+ }
package/dist/index.js ADDED
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var __async = (__this, __arguments, generator) => {
30
+ return new Promise((resolve, reject) => {
31
+ var fulfilled = (value) => {
32
+ try {
33
+ step(generator.next(value));
34
+ } catch (e) {
35
+ reject(e);
36
+ }
37
+ };
38
+ var rejected = (value) => {
39
+ try {
40
+ step(generator.throw(value));
41
+ } catch (e) {
42
+ reject(e);
43
+ }
44
+ };
45
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
46
+ step((generator = generator.apply(__this, __arguments)).next());
47
+ });
48
+ };
49
+
50
+ // src/index.ts
51
+ var src_exports = {};
52
+ __export(src_exports, {
53
+ fetch: () => wrappedFetch,
54
+ registerFetch: () => registerFetch
55
+ });
56
+ module.exports = __toCommonJS(src_exports);
57
+ var import_registerFunction = __toESM(require("@plasmicapp/host/registerFunction"));
58
+ function base64StringToBuffer(bstr) {
59
+ try {
60
+ bstr = atob(bstr);
61
+ } catch (e) {
62
+ throw new Error("Invalid base64 for binary type");
63
+ }
64
+ const uint8Array = new Uint8Array(bstr.length);
65
+ for (let i = 0; i < bstr.length; i++) {
66
+ uint8Array[i] = bstr.charCodeAt(i);
67
+ }
68
+ return uint8Array.buffer;
69
+ }
70
+ function bodyToFetchBody(body) {
71
+ if (body == null) {
72
+ return void 0;
73
+ } else if (typeof body === "object") {
74
+ return JSON.stringify(body);
75
+ } else if (body.startsWith("@binary")) {
76
+ return base64StringToBuffer(body.slice("@binary".length));
77
+ } else {
78
+ return body;
79
+ }
80
+ }
81
+ function maybeParseJSON(json) {
82
+ try {
83
+ return JSON.parse(json);
84
+ } catch (e) {
85
+ return json;
86
+ }
87
+ }
88
+ function wrappedFetch(url, method, headers, body) {
89
+ return __async(this, null, function* () {
90
+ const response = yield fetch(url, {
91
+ method,
92
+ headers,
93
+ body: bodyToFetchBody(body)
94
+ });
95
+ const statusCode = response.status;
96
+ const responseText = yield response.text();
97
+ return {
98
+ statusCode,
99
+ headers: Object.fromEntries(response.headers.entries()),
100
+ response: maybeParseJSON(responseText)
101
+ };
102
+ });
103
+ }
104
+ var registerFetchParams = {
105
+ name: "fetch",
106
+ importPath: "@plasmicapp/fetch",
107
+ params: [
108
+ {
109
+ name: "url",
110
+ type: "string"
111
+ },
112
+ {
113
+ name: "method",
114
+ type: "string"
115
+ },
116
+ {
117
+ name: "headers",
118
+ type: "object"
119
+ },
120
+ {
121
+ name: "body",
122
+ type: "object"
123
+ }
124
+ ]
125
+ };
126
+ function registerFetch(loader) {
127
+ if (loader) {
128
+ loader.registerFunction(wrappedFetch, registerFetchParams);
129
+ } else {
130
+ (0, import_registerFunction.default)(wrappedFetch, registerFetchParams);
131
+ }
132
+ }
133
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts"],
4
+ "sourcesContent": ["import registerFunction, {\n CustomFunctionMeta,\n} from \"@plasmicapp/host/registerFunction\";\n\ntype Registerable = {\n registerFunction: typeof registerFunction;\n};\n\n// Some functions were extracted from platform/wab/src/wab/server/data-sources/http-fetcher.ts\n\ntype HTTPMethod = \"GET\" | \"POST\" | \"PUT\" | \"DELETE\";\n\nfunction base64StringToBuffer(bstr: string) {\n try {\n bstr = atob(bstr);\n } catch (e) {\n throw new Error(\"Invalid base64 for binary type\");\n }\n const uint8Array = new Uint8Array(bstr.length);\n for (let i = 0; i < bstr.length; i++) {\n uint8Array[i] = bstr.charCodeAt(i);\n }\n return uint8Array.buffer;\n}\n\nfunction bodyToFetchBody(body?: string | object) {\n if (body == null) {\n return undefined;\n } else if (typeof body === \"object\") {\n return JSON.stringify(body);\n } else if (body.startsWith(\"@binary\")) {\n return base64StringToBuffer(body.slice(\"@binary\".length));\n } else {\n return body;\n }\n}\n\nfunction maybeParseJSON(json: string) {\n try {\n return JSON.parse(json);\n } catch (e) {\n return json;\n }\n}\n\nasync function wrappedFetch(\n url: string,\n method: HTTPMethod,\n headers: Record<string, string>,\n body?: string | object\n) {\n const response = await fetch(url, {\n method,\n headers,\n body: bodyToFetchBody(body),\n });\n\n const statusCode = response.status;\n const responseText = await response.text();\n\n return {\n statusCode,\n headers: Object.fromEntries(response.headers.entries()),\n response: maybeParseJSON(responseText),\n };\n}\n\nexport { wrappedFetch as fetch };\n\nconst registerFetchParams: CustomFunctionMeta<typeof wrappedFetch> = {\n name: \"fetch\",\n importPath: \"@plasmicapp/fetch\",\n params: [\n {\n name: \"url\",\n type: \"string\",\n },\n {\n name: \"method\",\n type: \"string\",\n },\n {\n name: \"headers\",\n type: \"object\",\n },\n {\n name: \"body\",\n type: \"object\",\n },\n ],\n};\n\nexport function registerFetch(loader?: Registerable) {\n if (loader) {\n loader.registerFunction(wrappedFetch, registerFetchParams);\n } else {\n registerFunction(wrappedFetch, registerFetchParams);\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,8BAEO;AAUP,SAAS,qBAAqB,MAAc;AAC1C,MAAI;AACF,WAAO,KAAK,IAAI;AAAA,EAClB,SAAS,GAAP;AACA,UAAM,IAAI,MAAM,gCAAgC;AAAA,EAClD;AACA,QAAM,aAAa,IAAI,WAAW,KAAK,MAAM;AAC7C,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,eAAW,CAAC,IAAI,KAAK,WAAW,C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ACF;",
6
+ "names": ["registerFunction"]
7
+ }
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@plasmicpkgs/fetch",
3
+ "version": "0.0.2",
4
+ "description": "Plasmic registration call for fetch function",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "module": "./dist/index.esm.js",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.esm.js",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "yarn build:types && yarn build:index",
20
+ "build:types": "yarn tsc",
21
+ "build:index": "node ../../build.mjs ./src/index.ts",
22
+ "prepublishOnly": "npm run build",
23
+ "postpublish": "bash ../../scripts/publish-api-doc-model.sh"
24
+ },
25
+ "license": "ISC",
26
+ "devDependencies": {
27
+ "typescript": "^5.7.3"
28
+ },
29
+ "peerDependencies": {
30
+ "@plasmicapp/host": "^1.0.211"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "gitHead": "6d7c17a75acd5c142edc510b1fb5f633f4f62a24"
36
+ }