@flock/wirespec 0.16.3 → 0.16.4
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/kotlin-kotlin-stdlib.mjs +11 -11
- package/kotlin-kotlin-stdlib.mjs.map +1 -1
- package/package.json +11 -1
- package/wirespec-fetch.d.ts +6 -0
- package/wirespec-fetch.mjs +32 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flock/wirespec",
|
|
3
|
-
"version": "0.16.
|
|
3
|
+
"version": "0.16.4",
|
|
4
4
|
"main": "wirespec-src-plugin-npm.mjs",
|
|
5
5
|
"types": "wirespec-src-plugin-npm.d.ts",
|
|
6
6
|
"devDependencies": {
|
|
@@ -17,6 +17,16 @@
|
|
|
17
17
|
"wirespec": "wirespec-bin.mjs"
|
|
18
18
|
},
|
|
19
19
|
"description": "Simplify your API development workflows, accelerate implementation, and guarantee strict adherence to defined contract specifications",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./wirespec-src-plugin-npm.d.ts",
|
|
23
|
+
"default": "./wirespec-src-plugin-npm.mjs"
|
|
24
|
+
},
|
|
25
|
+
"./fetch": {
|
|
26
|
+
"types": "./wirespec-fetch.d.ts",
|
|
27
|
+
"default": "./wirespec-fetch.mjs"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
20
30
|
"repository": {
|
|
21
31
|
"type": "git",
|
|
22
32
|
"url": "https://github.com/flock-community/wirespec"
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type Method = "GET" | "PUT" | "POST" | "DELETE" | "OPTIONS" | "HEAD" | "PATCH" | "TRACE"
|
|
2
|
+
export type RawRequest = { method: Method, path: string[], queries: Record<string, string>, headers: Record<string, string>, body?: string }
|
|
3
|
+
export type RawResponse = { status: number, headers: Record<string, string>, body?: string }
|
|
4
|
+
export type HandleFetch = ( path:string, init?:RequestInit) => Promise<Response>
|
|
5
|
+
|
|
6
|
+
export declare function wirespecFetch (rawRequest:RawRequest, handle?: HandleFetch): Promise<RawResponse>
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export async function wirespecFetch(req, handler) {
|
|
2
|
+
const contentHeader = req.body ? { 'Content-Type': 'application/json' } : {};
|
|
3
|
+
const body = req.body !== undefined ? req.body : undefined;
|
|
4
|
+
const query = Object.entries(req.queries)
|
|
5
|
+
.filter(([_, value]) => value !== undefined)
|
|
6
|
+
.flatMap(([key, value]) => {
|
|
7
|
+
if (value && typeof value === 'string' && value.startsWith('[') && value.endsWith(']')) {
|
|
8
|
+
const parsedValue = JSON.parse(value);
|
|
9
|
+
if (Array.isArray(parsedValue)) {
|
|
10
|
+
return parsedValue.map((item) => `${key}=${item}`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return `${key}=${value}`;
|
|
14
|
+
})
|
|
15
|
+
.join('&');
|
|
16
|
+
const url = `${req.path
|
|
17
|
+
.map(segment => encodeURIComponent(segment))
|
|
18
|
+
.join('/')}${query ? `?${query}` : ''}`;
|
|
19
|
+
const init = {method: req.method, body, headers: {...req.headers, ...contentHeader}}
|
|
20
|
+
const res = handler ? await handler(url, init) : await fetch(url, init)
|
|
21
|
+
const contentType = res.headers.get('Content-Type');
|
|
22
|
+
const contentLength = res.headers.get('Content-Length');
|
|
23
|
+
return {
|
|
24
|
+
status: res.status,
|
|
25
|
+
headers: {
|
|
26
|
+
...res.headers,
|
|
27
|
+
'Content-Type': contentType,
|
|
28
|
+
},
|
|
29
|
+
body: contentLength !== '0' && contentType ? await res.text() : undefined,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
}
|