@plasmicpkgs/fetch 0.0.5 → 0.0.7
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/dist/index.d.ts +1 -21
- package/dist/index.esm.js +9 -104
- package/dist/index.esm.js.map +2 -2
- package/dist/index.js +9 -102
- package/dist/index.js.map +2 -2
- package/package.json +11 -6
package/dist/index.d.ts
CHANGED
|
@@ -5,28 +5,10 @@ declare function fetch_2(url: string, method: HTTPMethod, headers: Record<string
|
|
|
5
5
|
headers: {
|
|
6
6
|
[k: string]: string;
|
|
7
7
|
};
|
|
8
|
-
|
|
8
|
+
body: any;
|
|
9
9
|
}>;
|
|
10
10
|
export { fetch_2 as fetch }
|
|
11
11
|
|
|
12
|
-
export declare interface FetchProps {
|
|
13
|
-
url?: string;
|
|
14
|
-
method?: string;
|
|
15
|
-
body?: string | object;
|
|
16
|
-
headers?: Record<string, string>;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export declare function graphqlFetch(url: string, method: HTTPMethod, headers: Record<string, string>, query?: {
|
|
20
|
-
query?: string;
|
|
21
|
-
variables?: object;
|
|
22
|
-
}, varOverrides?: Record<string, any>): Promise<{
|
|
23
|
-
statusCode: number;
|
|
24
|
-
headers: {
|
|
25
|
-
[k: string]: string;
|
|
26
|
-
};
|
|
27
|
-
response: any;
|
|
28
|
-
}>;
|
|
29
|
-
|
|
30
12
|
declare type HTTPMethod = "GET" | "POST" | "PUT" | "DELETE";
|
|
31
13
|
|
|
32
14
|
declare type Registerable = {
|
|
@@ -35,6 +17,4 @@ declare type Registerable = {
|
|
|
35
17
|
|
|
36
18
|
export declare function registerFetch(loader?: Registerable): void;
|
|
37
19
|
|
|
38
|
-
export declare function registerGraphqlFetch(loader?: Registerable): void;
|
|
39
|
-
|
|
40
20
|
export { }
|
package/dist/index.esm.js
CHANGED
|
@@ -1,22 +1,3 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
1
|
var __async = (__this, __arguments, generator) => {
|
|
21
2
|
return new Promise((resolve, reject) => {
|
|
22
3
|
var fulfilled = (value) => {
|
|
@@ -40,12 +21,12 @@ var __async = (__this, __arguments, generator) => {
|
|
|
40
21
|
|
|
41
22
|
// src/index.ts
|
|
42
23
|
import registerFunction from "@plasmicapp/host/registerFunction";
|
|
43
|
-
var
|
|
44
|
-
constructor(
|
|
45
|
-
super(
|
|
46
|
-
this.name = "CustomError";
|
|
24
|
+
var HttpError = class extends Error {
|
|
25
|
+
constructor(statusText, info, status) {
|
|
26
|
+
super(statusText);
|
|
47
27
|
this.info = info;
|
|
48
28
|
this.status = status;
|
|
29
|
+
this.name = "HttpError";
|
|
49
30
|
}
|
|
50
31
|
};
|
|
51
32
|
function base64StringToBuffer(bstr) {
|
|
@@ -78,8 +59,8 @@ function maybeParseJSON(json) {
|
|
|
78
59
|
return json;
|
|
79
60
|
}
|
|
80
61
|
}
|
|
81
|
-
function
|
|
82
|
-
return __async(this,
|
|
62
|
+
function wrappedFetch(url, method, headers, body) {
|
|
63
|
+
return __async(this, null, function* () {
|
|
83
64
|
if (!url) {
|
|
84
65
|
throw new Error("Please specify a URL to fetch");
|
|
85
66
|
}
|
|
@@ -103,20 +84,15 @@ function performFetch(_0) {
|
|
|
103
84
|
const text = yield response.text();
|
|
104
85
|
const maybeJson = maybeParseJSON(text);
|
|
105
86
|
if (!response.ok) {
|
|
106
|
-
throw new
|
|
87
|
+
throw new HttpError(response.statusText, maybeJson, response.status);
|
|
107
88
|
}
|
|
108
89
|
return {
|
|
109
90
|
statusCode: response.status,
|
|
110
91
|
headers: Object.fromEntries(response.headers.entries()),
|
|
111
|
-
|
|
92
|
+
body: maybeJson
|
|
112
93
|
};
|
|
113
94
|
});
|
|
114
95
|
}
|
|
115
|
-
function wrappedFetch(url, method, headers, body) {
|
|
116
|
-
return __async(this, null, function* () {
|
|
117
|
-
return yield performFetch({ url, method, headers, body });
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
96
|
var registerFetchParams = {
|
|
121
97
|
name: "fetch",
|
|
122
98
|
importPath: "@plasmicpkgs/fetch",
|
|
@@ -147,79 +123,8 @@ function registerFetch(loader) {
|
|
|
147
123
|
registerFunction(wrappedFetch, registerFetchParams);
|
|
148
124
|
}
|
|
149
125
|
}
|
|
150
|
-
function graphqlFetch(url, method, headers, query, varOverrides) {
|
|
151
|
-
return __async(this, null, function* () {
|
|
152
|
-
var _a;
|
|
153
|
-
let fetchProps;
|
|
154
|
-
method = method != null ? method : "POST";
|
|
155
|
-
if (method === "GET") {
|
|
156
|
-
const urlWithQueryParams = new URL(url != null ? url : "");
|
|
157
|
-
urlWithQueryParams.searchParams.set("query", (_a = query == null ? void 0 : query.query) != null ? _a : "{}");
|
|
158
|
-
urlWithQueryParams.searchParams.set(
|
|
159
|
-
"variables",
|
|
160
|
-
JSON.stringify(__spreadValues(__spreadValues({}, query == null ? void 0 : query.variables), varOverrides))
|
|
161
|
-
);
|
|
162
|
-
fetchProps = {
|
|
163
|
-
url: urlWithQueryParams.toString(),
|
|
164
|
-
method,
|
|
165
|
-
headers
|
|
166
|
-
};
|
|
167
|
-
} else {
|
|
168
|
-
fetchProps = {
|
|
169
|
-
body: __spreadProps(__spreadValues({}, query), { variables: __spreadValues(__spreadValues({}, query == null ? void 0 : query.variables), varOverrides) }),
|
|
170
|
-
url,
|
|
171
|
-
method,
|
|
172
|
-
headers
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
return performFetch(fetchProps);
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
var registerGraphqlFetchParams = {
|
|
179
|
-
name: "graphqlFetch",
|
|
180
|
-
importPath: "@plasmicpkgs/fetch",
|
|
181
|
-
params: [
|
|
182
|
-
{
|
|
183
|
-
name: "url",
|
|
184
|
-
type: "string"
|
|
185
|
-
},
|
|
186
|
-
{
|
|
187
|
-
name: "method",
|
|
188
|
-
type: "choice",
|
|
189
|
-
options: ["GET", "POST", "PUT", "DELETE"]
|
|
190
|
-
},
|
|
191
|
-
{
|
|
192
|
-
name: "headers",
|
|
193
|
-
type: "object"
|
|
194
|
-
},
|
|
195
|
-
{
|
|
196
|
-
name: "query",
|
|
197
|
-
type: "code",
|
|
198
|
-
lang: "graphql",
|
|
199
|
-
headers: (props) => props.headers,
|
|
200
|
-
endpoint: (props) => {
|
|
201
|
-
var _a;
|
|
202
|
-
return (_a = props.url) != null ? _a : "";
|
|
203
|
-
}
|
|
204
|
-
},
|
|
205
|
-
{
|
|
206
|
-
name: "varOverrides",
|
|
207
|
-
type: "object"
|
|
208
|
-
}
|
|
209
|
-
]
|
|
210
|
-
// TODO: remove as any when "code" type is available
|
|
211
|
-
};
|
|
212
|
-
function registerGraphqlFetch(loader) {
|
|
213
|
-
if (loader) {
|
|
214
|
-
loader.registerFunction(graphqlFetch, registerGraphqlFetchParams);
|
|
215
|
-
} else {
|
|
216
|
-
registerFunction(graphqlFetch, registerGraphqlFetchParams);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
126
|
export {
|
|
220
127
|
wrappedFetch as fetch,
|
|
221
|
-
|
|
222
|
-
registerFetch,
|
|
223
|
-
registerGraphqlFetch
|
|
128
|
+
registerFetch
|
|
224
129
|
};
|
|
225
130
|
//# sourceMappingURL=index.esm.js.map
|
package/dist/index.esm.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
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\nclass
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import registerFunction, {\n CustomFunctionMeta,\n} from \"@plasmicapp/host/registerFunction\";\n\ntype Registerable = {\n registerFunction: typeof registerFunction;\n};\n\nclass HttpError extends Error {\n constructor(\n statusText: string,\n readonly info: unknown,\n readonly status: number\n ) {\n super(statusText);\n this.name = \"HttpError\";\n }\n}\n\n// Some functions were extracted from platform/wab/src/wab/server/data-sources/http-fetcher.ts\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\n// Don't override the global fetch\nasync function wrappedFetch(\n url: string,\n method: HTTPMethod,\n headers: Record<string, string>,\n body?: string | object\n) {\n if (!url) {\n throw new Error(\"Please specify a URL to fetch\");\n }\n\n // Add default headers unless specified\n if (!headers) {\n headers = {};\n }\n const headerNamesLowercase = new Set(\n Object.keys(headers).map((headerName) => headerName.toLowerCase())\n );\n if (!headerNamesLowercase.has(\"accept\")) {\n headers[\"Accept\"] = \"application/json\";\n }\n if (body && !headerNamesLowercase.has(\"content-type\")) {\n headers[\"Content-Type\"] = \"application/json\";\n }\n\n const response = await fetch(url, {\n method,\n headers,\n body: bodyToFetchBody(body),\n });\n\n const text = await response.text();\n const maybeJson = maybeParseJSON(text);\n\n // @see https://swr.vercel.app/docs/error-handling\n // If the status code is not in the range 200-299,\n // we still try to parse and throw it.\n if (!response.ok) {\n throw new HttpError(response.statusText, maybeJson, response.status);\n }\n\n return {\n statusCode: response.status,\n headers: Object.fromEntries(response.headers.entries()),\n body: maybeJson,\n };\n}\n\nexport { wrappedFetch as fetch };\n\nconst registerFetchParams: CustomFunctionMeta<typeof wrappedFetch> = {\n name: \"fetch\",\n importPath: \"@plasmicpkgs/fetch\",\n params: [\n {\n name: \"url\",\n type: \"string\",\n },\n {\n name: \"method\",\n type: \"choice\",\n options: [\"GET\", \"POST\", \"PUT\", \"DELETE\"],\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;AAMP,IAAM,YAAN,cAAwB,MAAM;AAAA,EAC5B,YACE,YACS,MACA,QACT;AACA,UAAM,UAAU;AAHP;AACA;AAGT,SAAK,OAAO;AAAA,EACd;AACF;AAKA,SAAS,qBAAqB,MAAc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AAM,SAAS,KAAK;AACjC,UAAM,YAAY,eAAe,IAAI;AAKrC,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,IAAI,UAAU,SAAS,YAAY,WAAW,SAAS,MAAM;AAAA,IACrE;AAEA,WAAO;AAAA,MACL,YAAY,SAAS;AAAA,MACrB,SAAS,OAAO,YAAY,SAAS,QAAQ,QAAQ,CAAC;AAAA,MACtD,MAAM;AAAA,IACR;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,MACN,SAAS,CAAC,OAAO,QAAQ,OAAO,QAAQ;AAAA,IAC1C;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
6
|
"names": []
|
|
7
7
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,27 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __create = Object.create;
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
|
-
var __defProps = Object.defineProperties;
|
|
5
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
7
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
10
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
-
var __spreadValues = (a, b) => {
|
|
14
|
-
for (var prop in b || (b = {}))
|
|
15
|
-
if (__hasOwnProp.call(b, prop))
|
|
16
|
-
__defNormalProp(a, prop, b[prop]);
|
|
17
|
-
if (__getOwnPropSymbols)
|
|
18
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
-
if (__propIsEnum.call(b, prop))
|
|
20
|
-
__defNormalProp(a, prop, b[prop]);
|
|
21
|
-
}
|
|
22
|
-
return a;
|
|
23
|
-
};
|
|
24
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
8
|
var __export = (target, all) => {
|
|
26
9
|
for (var name in all)
|
|
27
10
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -68,18 +51,16 @@ var __async = (__this, __arguments, generator) => {
|
|
|
68
51
|
var src_exports = {};
|
|
69
52
|
__export(src_exports, {
|
|
70
53
|
fetch: () => wrappedFetch,
|
|
71
|
-
|
|
72
|
-
registerFetch: () => registerFetch,
|
|
73
|
-
registerGraphqlFetch: () => registerGraphqlFetch
|
|
54
|
+
registerFetch: () => registerFetch
|
|
74
55
|
});
|
|
75
56
|
module.exports = __toCommonJS(src_exports);
|
|
76
57
|
var import_registerFunction = __toESM(require("@plasmicapp/host/registerFunction"));
|
|
77
|
-
var
|
|
78
|
-
constructor(
|
|
79
|
-
super(
|
|
80
|
-
this.name = "CustomError";
|
|
58
|
+
var HttpError = class extends Error {
|
|
59
|
+
constructor(statusText, info, status) {
|
|
60
|
+
super(statusText);
|
|
81
61
|
this.info = info;
|
|
82
62
|
this.status = status;
|
|
63
|
+
this.name = "HttpError";
|
|
83
64
|
}
|
|
84
65
|
};
|
|
85
66
|
function base64StringToBuffer(bstr) {
|
|
@@ -112,8 +93,8 @@ function maybeParseJSON(json) {
|
|
|
112
93
|
return json;
|
|
113
94
|
}
|
|
114
95
|
}
|
|
115
|
-
function
|
|
116
|
-
return __async(this,
|
|
96
|
+
function wrappedFetch(url, method, headers, body) {
|
|
97
|
+
return __async(this, null, function* () {
|
|
117
98
|
if (!url) {
|
|
118
99
|
throw new Error("Please specify a URL to fetch");
|
|
119
100
|
}
|
|
@@ -137,20 +118,15 @@ function performFetch(_0) {
|
|
|
137
118
|
const text = yield response.text();
|
|
138
119
|
const maybeJson = maybeParseJSON(text);
|
|
139
120
|
if (!response.ok) {
|
|
140
|
-
throw new
|
|
121
|
+
throw new HttpError(response.statusText, maybeJson, response.status);
|
|
141
122
|
}
|
|
142
123
|
return {
|
|
143
124
|
statusCode: response.status,
|
|
144
125
|
headers: Object.fromEntries(response.headers.entries()),
|
|
145
|
-
|
|
126
|
+
body: maybeJson
|
|
146
127
|
};
|
|
147
128
|
});
|
|
148
129
|
}
|
|
149
|
-
function wrappedFetch(url, method, headers, body) {
|
|
150
|
-
return __async(this, null, function* () {
|
|
151
|
-
return yield performFetch({ url, method, headers, body });
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
130
|
var registerFetchParams = {
|
|
155
131
|
name: "fetch",
|
|
156
132
|
importPath: "@plasmicpkgs/fetch",
|
|
@@ -181,73 +157,4 @@ function registerFetch(loader) {
|
|
|
181
157
|
(0, import_registerFunction.default)(wrappedFetch, registerFetchParams);
|
|
182
158
|
}
|
|
183
159
|
}
|
|
184
|
-
function graphqlFetch(url, method, headers, query, varOverrides) {
|
|
185
|
-
return __async(this, null, function* () {
|
|
186
|
-
var _a;
|
|
187
|
-
let fetchProps;
|
|
188
|
-
method = method != null ? method : "POST";
|
|
189
|
-
if (method === "GET") {
|
|
190
|
-
const urlWithQueryParams = new URL(url != null ? url : "");
|
|
191
|
-
urlWithQueryParams.searchParams.set("query", (_a = query == null ? void 0 : query.query) != null ? _a : "{}");
|
|
192
|
-
urlWithQueryParams.searchParams.set(
|
|
193
|
-
"variables",
|
|
194
|
-
JSON.stringify(__spreadValues(__spreadValues({}, query == null ? void 0 : query.variables), varOverrides))
|
|
195
|
-
);
|
|
196
|
-
fetchProps = {
|
|
197
|
-
url: urlWithQueryParams.toString(),
|
|
198
|
-
method,
|
|
199
|
-
headers
|
|
200
|
-
};
|
|
201
|
-
} else {
|
|
202
|
-
fetchProps = {
|
|
203
|
-
body: __spreadProps(__spreadValues({}, query), { variables: __spreadValues(__spreadValues({}, query == null ? void 0 : query.variables), varOverrides) }),
|
|
204
|
-
url,
|
|
205
|
-
method,
|
|
206
|
-
headers
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
return performFetch(fetchProps);
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
var registerGraphqlFetchParams = {
|
|
213
|
-
name: "graphqlFetch",
|
|
214
|
-
importPath: "@plasmicpkgs/fetch",
|
|
215
|
-
params: [
|
|
216
|
-
{
|
|
217
|
-
name: "url",
|
|
218
|
-
type: "string"
|
|
219
|
-
},
|
|
220
|
-
{
|
|
221
|
-
name: "method",
|
|
222
|
-
type: "choice",
|
|
223
|
-
options: ["GET", "POST", "PUT", "DELETE"]
|
|
224
|
-
},
|
|
225
|
-
{
|
|
226
|
-
name: "headers",
|
|
227
|
-
type: "object"
|
|
228
|
-
},
|
|
229
|
-
{
|
|
230
|
-
name: "query",
|
|
231
|
-
type: "code",
|
|
232
|
-
lang: "graphql",
|
|
233
|
-
headers: (props) => props.headers,
|
|
234
|
-
endpoint: (props) => {
|
|
235
|
-
var _a;
|
|
236
|
-
return (_a = props.url) != null ? _a : "";
|
|
237
|
-
}
|
|
238
|
-
},
|
|
239
|
-
{
|
|
240
|
-
name: "varOverrides",
|
|
241
|
-
type: "object"
|
|
242
|
-
}
|
|
243
|
-
]
|
|
244
|
-
// TODO: remove as any when "code" type is available
|
|
245
|
-
};
|
|
246
|
-
function registerGraphqlFetch(loader) {
|
|
247
|
-
if (loader) {
|
|
248
|
-
loader.registerFunction(graphqlFetch, registerGraphqlFetchParams);
|
|
249
|
-
} else {
|
|
250
|
-
(0, import_registerFunction.default)(graphqlFetch, registerGraphqlFetchParams);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
160
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
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\nclass
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import registerFunction, {\n CustomFunctionMeta,\n} from \"@plasmicapp/host/registerFunction\";\n\ntype Registerable = {\n registerFunction: typeof registerFunction;\n};\n\nclass HttpError extends Error {\n constructor(\n statusText: string,\n readonly info: unknown,\n readonly status: number\n ) {\n super(statusText);\n this.name = \"HttpError\";\n }\n}\n\n// Some functions were extracted from platform/wab/src/wab/server/data-sources/http-fetcher.ts\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\n// Don't override the global fetch\nasync function wrappedFetch(\n url: string,\n method: HTTPMethod,\n headers: Record<string, string>,\n body?: string | object\n) {\n if (!url) {\n throw new Error(\"Please specify a URL to fetch\");\n }\n\n // Add default headers unless specified\n if (!headers) {\n headers = {};\n }\n const headerNamesLowercase = new Set(\n Object.keys(headers).map((headerName) => headerName.toLowerCase())\n );\n if (!headerNamesLowercase.has(\"accept\")) {\n headers[\"Accept\"] = \"application/json\";\n }\n if (body && !headerNamesLowercase.has(\"content-type\")) {\n headers[\"Content-Type\"] = \"application/json\";\n }\n\n const response = await fetch(url, {\n method,\n headers,\n body: bodyToFetchBody(body),\n });\n\n const text = await response.text();\n const maybeJson = maybeParseJSON(text);\n\n // @see https://swr.vercel.app/docs/error-handling\n // If the status code is not in the range 200-299,\n // we still try to parse and throw it.\n if (!response.ok) {\n throw new HttpError(response.statusText, maybeJson, response.status);\n }\n\n return {\n statusCode: response.status,\n headers: Object.fromEntries(response.headers.entries()),\n body: maybeJson,\n };\n}\n\nexport { wrappedFetch as fetch };\n\nconst registerFetchParams: CustomFunctionMeta<typeof wrappedFetch> = {\n name: \"fetch\",\n importPath: \"@plasmicpkgs/fetch\",\n params: [\n {\n name: \"url\",\n type: \"string\",\n },\n {\n name: \"method\",\n type: \"choice\",\n options: [\"GET\", \"POST\", \"PUT\", \"DELETE\"],\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;AAMP,IAAM,YAAN,cAAwB,MAAM;AAAA,EAC5B,YACE,YACS,MACA,QACT;AACA,UAAM,UAAU;AAHP;AACA;AAGT,SAAK,OAAO;AAAA,EACd;AACF;AAKA,SAAS,qBA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M,OAAO,MAAM,SAAS,KAAK;AACjC,UAAM,YAAY,eAAe,IAAI;AAKrC,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,IAAI,UAAU,SAAS,YAAY,WAAW,SAAS,MAAM;AAAA,IACrE;AAEA,WAAO;AAAA,MACL,YAAY,SAAS;AAAA,MACrB,SAAS,OAAO,YAAY,SAAS,QAAQ,QAAQ,CAAC;AAAA,MACtD,MAAM;AAAA,IACR;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,MACN,SAAS,CAAC,OAAO,QAAQ,OAAO,QAAQ;AAAA,IAC1C;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,gCAAAA,SAAiB,cAAc,mBAAmB;AAAA,EACpD;AACF;",
|
|
6
6
|
"names": ["registerFunction"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plasmicpkgs/fetch",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "Plasmic registration call for fetch function",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/plasmicapp/plasmic.git",
|
|
8
|
+
"directory": "plasmicpkgs/fetch"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
5
13
|
"main": "./dist/index.js",
|
|
6
14
|
"types": "./dist/index.d.ts",
|
|
7
15
|
"module": "./dist/index.esm.js",
|
|
@@ -22,15 +30,12 @@
|
|
|
22
30
|
"prepublishOnly": "npm run build",
|
|
23
31
|
"postpublish": "bash ../../scripts/publish-api-doc-model.sh"
|
|
24
32
|
},
|
|
25
|
-
"license": "ISC",
|
|
26
33
|
"devDependencies": {
|
|
34
|
+
"@plasmicapp/host": "1.0.217",
|
|
27
35
|
"typescript": "^5.7.3"
|
|
28
36
|
},
|
|
29
37
|
"peerDependencies": {
|
|
30
38
|
"@plasmicapp/host": "^1.0.211"
|
|
31
39
|
},
|
|
32
|
-
"
|
|
33
|
-
"access": "public"
|
|
34
|
-
},
|
|
35
|
-
"gitHead": "e25172b4452e1a8405c93cfc7fd54e531ecab146"
|
|
40
|
+
"gitHead": "99eedda0d26a21d13b5b6887f85d3c1f34740760"
|
|
36
41
|
}
|