@opencode-ai/sdk 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/gen/client/client.d.ts +2 -0
- package/dist/gen/client/client.js +145 -0
- package/dist/gen/client/index.d.ts +7 -0
- package/dist/gen/client/index.js +4 -0
- package/dist/gen/client/types.d.ts +119 -0
- package/dist/gen/client/types.js +0 -0
- package/dist/gen/client/utils.d.ts +45 -0
- package/dist/gen/client/utils.js +284 -0
- package/dist/gen/client.gen.d.ts +12 -0
- package/dist/gen/client.gen.js +5 -0
- package/dist/gen/core/auth.d.ts +18 -0
- package/dist/gen/core/auth.js +13 -0
- package/dist/gen/core/bodySerializer.d.ts +17 -0
- package/dist/gen/core/bodySerializer.js +53 -0
- package/dist/gen/core/params.d.ts +33 -0
- package/dist/gen/core/params.js +87 -0
- package/dist/gen/core/pathSerializer.d.ts +33 -0
- package/dist/gen/core/pathSerializer.js +113 -0
- package/dist/gen/core/types.d.ts +78 -0
- package/dist/gen/core/types.js +0 -0
- package/dist/gen/sdk.gen.d.ts +149 -0
- package/dist/gen/sdk.gen.js +291 -0
- package/dist/gen/types.gen.d.ts +1308 -0
- package/dist/gen/types.gen.js +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +6 -0
- package/dist/script/generate.js +31 -0
- package/dist/script/publish.js +4 -0
- package/dist/src/gen/client/client.js +145 -0
- package/dist/src/gen/client/index.js +4 -0
- package/dist/src/gen/client/types.js +0 -0
- package/dist/src/gen/client/utils.js +284 -0
- package/dist/src/gen/client.gen.js +5 -0
- package/dist/src/gen/core/auth.js +13 -0
- package/dist/src/gen/core/bodySerializer.js +53 -0
- package/dist/src/gen/core/params.js +87 -0
- package/dist/src/gen/core/pathSerializer.js +113 -0
- package/dist/src/gen/core/types.js +0 -0
- package/dist/src/gen/sdk.gen.js +291 -0
- package/dist/src/gen/types.gen.js +1 -0
- package/dist/src/index.js +6 -0
- package/package.json +17 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// This file is auto-generated by @hey-api/openapi-ts
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import { $ } from "bun";
|
|
3
|
+
import fs from "fs/promises";
|
|
4
|
+
import path from "path";
|
|
5
|
+
console.log("=== Generating JS SDK ===");
|
|
6
|
+
console.log();
|
|
7
|
+
import { createClient } from "@hey-api/openapi-ts";
|
|
8
|
+
const dir = new URL("..", import.meta.url).pathname;
|
|
9
|
+
await fs.rm(path.join(dir, "src/gen"), { recursive: true, force: true });
|
|
10
|
+
await $ `bun run ../../opencode/src/index.ts generate > openapi.json`.cwd(dir);
|
|
11
|
+
await createClient({
|
|
12
|
+
input: "./openapi.json",
|
|
13
|
+
output: "./src/gen",
|
|
14
|
+
plugins: [
|
|
15
|
+
{
|
|
16
|
+
name: "@hey-api/typescript",
|
|
17
|
+
exportFromIndex: false,
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
name: "@hey-api/sdk",
|
|
21
|
+
instance: "OpencodeClient",
|
|
22
|
+
exportFromIndex: false,
|
|
23
|
+
auth: false,
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: "@hey-api/client-fetch",
|
|
27
|
+
exportFromIndex: false,
|
|
28
|
+
baseUrl: "http://localhost:4096",
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { buildUrl, createConfig, createInterceptors, getParseAs, mergeConfigs, mergeHeaders, setAuthParams, } from './utils';
|
|
2
|
+
export const createClient = (config = {}) => {
|
|
3
|
+
let _config = mergeConfigs(createConfig(), config);
|
|
4
|
+
const getConfig = () => ({ ..._config });
|
|
5
|
+
const setConfig = (config) => {
|
|
6
|
+
_config = mergeConfigs(_config, config);
|
|
7
|
+
return getConfig();
|
|
8
|
+
};
|
|
9
|
+
const interceptors = createInterceptors();
|
|
10
|
+
const request = async (options) => {
|
|
11
|
+
const opts = {
|
|
12
|
+
..._config,
|
|
13
|
+
...options,
|
|
14
|
+
fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
|
|
15
|
+
headers: mergeHeaders(_config.headers, options.headers),
|
|
16
|
+
};
|
|
17
|
+
if (opts.security) {
|
|
18
|
+
await setAuthParams({
|
|
19
|
+
...opts,
|
|
20
|
+
security: opts.security,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
if (opts.requestValidator) {
|
|
24
|
+
await opts.requestValidator(opts);
|
|
25
|
+
}
|
|
26
|
+
if (opts.body && opts.bodySerializer) {
|
|
27
|
+
opts.body = opts.bodySerializer(opts.body);
|
|
28
|
+
}
|
|
29
|
+
// remove Content-Type header if body is empty to avoid sending invalid requests
|
|
30
|
+
if (opts.body === undefined || opts.body === '') {
|
|
31
|
+
opts.headers.delete('Content-Type');
|
|
32
|
+
}
|
|
33
|
+
const url = buildUrl(opts);
|
|
34
|
+
const requestInit = {
|
|
35
|
+
redirect: 'follow',
|
|
36
|
+
...opts,
|
|
37
|
+
};
|
|
38
|
+
let request = new Request(url, requestInit);
|
|
39
|
+
for (const fn of interceptors.request._fns) {
|
|
40
|
+
if (fn) {
|
|
41
|
+
request = await fn(request, opts);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// fetch must be assigned here, otherwise it would throw the error:
|
|
45
|
+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
|
|
46
|
+
const _fetch = opts.fetch;
|
|
47
|
+
let response = await _fetch(request);
|
|
48
|
+
for (const fn of interceptors.response._fns) {
|
|
49
|
+
if (fn) {
|
|
50
|
+
response = await fn(response, request, opts);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const result = {
|
|
54
|
+
request,
|
|
55
|
+
response,
|
|
56
|
+
};
|
|
57
|
+
if (response.ok) {
|
|
58
|
+
if (response.status === 204 ||
|
|
59
|
+
response.headers.get('Content-Length') === '0') {
|
|
60
|
+
return opts.responseStyle === 'data'
|
|
61
|
+
? {}
|
|
62
|
+
: {
|
|
63
|
+
data: {},
|
|
64
|
+
...result,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
const parseAs = (opts.parseAs === 'auto'
|
|
68
|
+
? getParseAs(response.headers.get('Content-Type'))
|
|
69
|
+
: opts.parseAs) ?? 'json';
|
|
70
|
+
let data;
|
|
71
|
+
switch (parseAs) {
|
|
72
|
+
case 'arrayBuffer':
|
|
73
|
+
case 'blob':
|
|
74
|
+
case 'formData':
|
|
75
|
+
case 'json':
|
|
76
|
+
case 'text':
|
|
77
|
+
data = await response[parseAs]();
|
|
78
|
+
break;
|
|
79
|
+
case 'stream':
|
|
80
|
+
return opts.responseStyle === 'data'
|
|
81
|
+
? response.body
|
|
82
|
+
: {
|
|
83
|
+
data: response.body,
|
|
84
|
+
...result,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
if (parseAs === 'json') {
|
|
88
|
+
if (opts.responseValidator) {
|
|
89
|
+
await opts.responseValidator(data);
|
|
90
|
+
}
|
|
91
|
+
if (opts.responseTransformer) {
|
|
92
|
+
data = await opts.responseTransformer(data);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return opts.responseStyle === 'data'
|
|
96
|
+
? data
|
|
97
|
+
: {
|
|
98
|
+
data,
|
|
99
|
+
...result,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const textError = await response.text();
|
|
103
|
+
let jsonError;
|
|
104
|
+
try {
|
|
105
|
+
jsonError = JSON.parse(textError);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
// noop
|
|
109
|
+
}
|
|
110
|
+
const error = jsonError ?? textError;
|
|
111
|
+
let finalError = error;
|
|
112
|
+
for (const fn of interceptors.error._fns) {
|
|
113
|
+
if (fn) {
|
|
114
|
+
finalError = (await fn(error, response, request, opts));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
finalError = finalError || {};
|
|
118
|
+
if (opts.throwOnError) {
|
|
119
|
+
throw finalError;
|
|
120
|
+
}
|
|
121
|
+
// TODO: we probably want to return error and improve types
|
|
122
|
+
return opts.responseStyle === 'data'
|
|
123
|
+
? undefined
|
|
124
|
+
: {
|
|
125
|
+
error: finalError,
|
|
126
|
+
...result,
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
return {
|
|
130
|
+
buildUrl,
|
|
131
|
+
connect: (options) => request({ ...options, method: 'CONNECT' }),
|
|
132
|
+
delete: (options) => request({ ...options, method: 'DELETE' }),
|
|
133
|
+
get: (options) => request({ ...options, method: 'GET' }),
|
|
134
|
+
getConfig,
|
|
135
|
+
head: (options) => request({ ...options, method: 'HEAD' }),
|
|
136
|
+
interceptors,
|
|
137
|
+
options: (options) => request({ ...options, method: 'OPTIONS' }),
|
|
138
|
+
patch: (options) => request({ ...options, method: 'PATCH' }),
|
|
139
|
+
post: (options) => request({ ...options, method: 'POST' }),
|
|
140
|
+
put: (options) => request({ ...options, method: 'PUT' }),
|
|
141
|
+
request,
|
|
142
|
+
setConfig,
|
|
143
|
+
trace: (options) => request({ ...options, method: 'TRACE' }),
|
|
144
|
+
};
|
|
145
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { getAuthToken } from '../core/auth';
|
|
2
|
+
import { jsonBodySerializer } from '../core/bodySerializer';
|
|
3
|
+
import { serializeArrayParam, serializeObjectParam, serializePrimitiveParam, } from '../core/pathSerializer';
|
|
4
|
+
const PATH_PARAM_RE = /\{[^{}]+\}/g;
|
|
5
|
+
const defaultPathSerializer = ({ path, url: _url }) => {
|
|
6
|
+
let url = _url;
|
|
7
|
+
const matches = _url.match(PATH_PARAM_RE);
|
|
8
|
+
if (matches) {
|
|
9
|
+
for (const match of matches) {
|
|
10
|
+
let explode = false;
|
|
11
|
+
let name = match.substring(1, match.length - 1);
|
|
12
|
+
let style = 'simple';
|
|
13
|
+
if (name.endsWith('*')) {
|
|
14
|
+
explode = true;
|
|
15
|
+
name = name.substring(0, name.length - 1);
|
|
16
|
+
}
|
|
17
|
+
if (name.startsWith('.')) {
|
|
18
|
+
name = name.substring(1);
|
|
19
|
+
style = 'label';
|
|
20
|
+
}
|
|
21
|
+
else if (name.startsWith(';')) {
|
|
22
|
+
name = name.substring(1);
|
|
23
|
+
style = 'matrix';
|
|
24
|
+
}
|
|
25
|
+
const value = path[name];
|
|
26
|
+
if (value === undefined || value === null) {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (Array.isArray(value)) {
|
|
30
|
+
url = url.replace(match, serializeArrayParam({ explode, name, style, value }));
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (typeof value === 'object') {
|
|
34
|
+
url = url.replace(match, serializeObjectParam({
|
|
35
|
+
explode,
|
|
36
|
+
name,
|
|
37
|
+
style,
|
|
38
|
+
value: value,
|
|
39
|
+
valueOnly: true,
|
|
40
|
+
}));
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (style === 'matrix') {
|
|
44
|
+
url = url.replace(match, `;${serializePrimitiveParam({
|
|
45
|
+
name,
|
|
46
|
+
value: value,
|
|
47
|
+
})}`);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const replaceValue = encodeURIComponent(style === 'label' ? `.${value}` : value);
|
|
51
|
+
url = url.replace(match, replaceValue);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return url;
|
|
55
|
+
};
|
|
56
|
+
export const createQuerySerializer = ({ allowReserved, array, object, } = {}) => {
|
|
57
|
+
const querySerializer = (queryParams) => {
|
|
58
|
+
const search = [];
|
|
59
|
+
if (queryParams && typeof queryParams === 'object') {
|
|
60
|
+
for (const name in queryParams) {
|
|
61
|
+
const value = queryParams[name];
|
|
62
|
+
if (value === undefined || value === null) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (Array.isArray(value)) {
|
|
66
|
+
const serializedArray = serializeArrayParam({
|
|
67
|
+
allowReserved,
|
|
68
|
+
explode: true,
|
|
69
|
+
name,
|
|
70
|
+
style: 'form',
|
|
71
|
+
value,
|
|
72
|
+
...array,
|
|
73
|
+
});
|
|
74
|
+
if (serializedArray)
|
|
75
|
+
search.push(serializedArray);
|
|
76
|
+
}
|
|
77
|
+
else if (typeof value === 'object') {
|
|
78
|
+
const serializedObject = serializeObjectParam({
|
|
79
|
+
allowReserved,
|
|
80
|
+
explode: true,
|
|
81
|
+
name,
|
|
82
|
+
style: 'deepObject',
|
|
83
|
+
value: value,
|
|
84
|
+
...object,
|
|
85
|
+
});
|
|
86
|
+
if (serializedObject)
|
|
87
|
+
search.push(serializedObject);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
const serializedPrimitive = serializePrimitiveParam({
|
|
91
|
+
allowReserved,
|
|
92
|
+
name,
|
|
93
|
+
value: value,
|
|
94
|
+
});
|
|
95
|
+
if (serializedPrimitive)
|
|
96
|
+
search.push(serializedPrimitive);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return search.join('&');
|
|
101
|
+
};
|
|
102
|
+
return querySerializer;
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Infers parseAs value from provided Content-Type header.
|
|
106
|
+
*/
|
|
107
|
+
export const getParseAs = (contentType) => {
|
|
108
|
+
if (!contentType) {
|
|
109
|
+
// If no Content-Type header is provided, the best we can do is return the raw response body,
|
|
110
|
+
// which is effectively the same as the 'stream' option.
|
|
111
|
+
return 'stream';
|
|
112
|
+
}
|
|
113
|
+
const cleanContent = contentType.split(';')[0]?.trim();
|
|
114
|
+
if (!cleanContent) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (cleanContent.startsWith('application/json') ||
|
|
118
|
+
cleanContent.endsWith('+json')) {
|
|
119
|
+
return 'json';
|
|
120
|
+
}
|
|
121
|
+
if (cleanContent === 'multipart/form-data') {
|
|
122
|
+
return 'formData';
|
|
123
|
+
}
|
|
124
|
+
if (['application/', 'audio/', 'image/', 'video/'].some((type) => cleanContent.startsWith(type))) {
|
|
125
|
+
return 'blob';
|
|
126
|
+
}
|
|
127
|
+
if (cleanContent.startsWith('text/')) {
|
|
128
|
+
return 'text';
|
|
129
|
+
}
|
|
130
|
+
return;
|
|
131
|
+
};
|
|
132
|
+
export const setAuthParams = async ({ security, ...options }) => {
|
|
133
|
+
for (const auth of security) {
|
|
134
|
+
const token = await getAuthToken(auth, options.auth);
|
|
135
|
+
if (!token) {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const name = auth.name ?? 'Authorization';
|
|
139
|
+
switch (auth.in) {
|
|
140
|
+
case 'query':
|
|
141
|
+
if (!options.query) {
|
|
142
|
+
options.query = {};
|
|
143
|
+
}
|
|
144
|
+
options.query[name] = token;
|
|
145
|
+
break;
|
|
146
|
+
case 'cookie':
|
|
147
|
+
options.headers.append('Cookie', `${name}=${token}`);
|
|
148
|
+
break;
|
|
149
|
+
case 'header':
|
|
150
|
+
default:
|
|
151
|
+
options.headers.set(name, token);
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
export const buildUrl = (options) => {
|
|
158
|
+
const url = getUrl({
|
|
159
|
+
baseUrl: options.baseUrl,
|
|
160
|
+
path: options.path,
|
|
161
|
+
query: options.query,
|
|
162
|
+
querySerializer: typeof options.querySerializer === 'function'
|
|
163
|
+
? options.querySerializer
|
|
164
|
+
: createQuerySerializer(options.querySerializer),
|
|
165
|
+
url: options.url,
|
|
166
|
+
});
|
|
167
|
+
return url;
|
|
168
|
+
};
|
|
169
|
+
export const getUrl = ({ baseUrl, path, query, querySerializer, url: _url, }) => {
|
|
170
|
+
const pathUrl = _url.startsWith('/') ? _url : `/${_url}`;
|
|
171
|
+
let url = (baseUrl ?? '') + pathUrl;
|
|
172
|
+
if (path) {
|
|
173
|
+
url = defaultPathSerializer({ path, url });
|
|
174
|
+
}
|
|
175
|
+
let search = query ? querySerializer(query) : '';
|
|
176
|
+
if (search.startsWith('?')) {
|
|
177
|
+
search = search.substring(1);
|
|
178
|
+
}
|
|
179
|
+
if (search) {
|
|
180
|
+
url += `?${search}`;
|
|
181
|
+
}
|
|
182
|
+
return url;
|
|
183
|
+
};
|
|
184
|
+
export const mergeConfigs = (a, b) => {
|
|
185
|
+
const config = { ...a, ...b };
|
|
186
|
+
if (config.baseUrl?.endsWith('/')) {
|
|
187
|
+
config.baseUrl = config.baseUrl.substring(0, config.baseUrl.length - 1);
|
|
188
|
+
}
|
|
189
|
+
config.headers = mergeHeaders(a.headers, b.headers);
|
|
190
|
+
return config;
|
|
191
|
+
};
|
|
192
|
+
export const mergeHeaders = (...headers) => {
|
|
193
|
+
const mergedHeaders = new Headers();
|
|
194
|
+
for (const header of headers) {
|
|
195
|
+
if (!header || typeof header !== 'object') {
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
const iterator = header instanceof Headers ? header.entries() : Object.entries(header);
|
|
199
|
+
for (const [key, value] of iterator) {
|
|
200
|
+
if (value === null) {
|
|
201
|
+
mergedHeaders.delete(key);
|
|
202
|
+
}
|
|
203
|
+
else if (Array.isArray(value)) {
|
|
204
|
+
for (const v of value) {
|
|
205
|
+
mergedHeaders.append(key, v);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
else if (value !== undefined) {
|
|
209
|
+
// assume object headers are meant to be JSON stringified, i.e. their
|
|
210
|
+
// content value in OpenAPI specification is 'application/json'
|
|
211
|
+
mergedHeaders.set(key, typeof value === 'object' ? JSON.stringify(value) : value);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return mergedHeaders;
|
|
216
|
+
};
|
|
217
|
+
class Interceptors {
|
|
218
|
+
_fns;
|
|
219
|
+
constructor() {
|
|
220
|
+
this._fns = [];
|
|
221
|
+
}
|
|
222
|
+
clear() {
|
|
223
|
+
this._fns = [];
|
|
224
|
+
}
|
|
225
|
+
getInterceptorIndex(id) {
|
|
226
|
+
if (typeof id === 'number') {
|
|
227
|
+
return this._fns[id] ? id : -1;
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
return this._fns.indexOf(id);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
exists(id) {
|
|
234
|
+
const index = this.getInterceptorIndex(id);
|
|
235
|
+
return !!this._fns[index];
|
|
236
|
+
}
|
|
237
|
+
eject(id) {
|
|
238
|
+
const index = this.getInterceptorIndex(id);
|
|
239
|
+
if (this._fns[index]) {
|
|
240
|
+
this._fns[index] = null;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
update(id, fn) {
|
|
244
|
+
const index = this.getInterceptorIndex(id);
|
|
245
|
+
if (this._fns[index]) {
|
|
246
|
+
this._fns[index] = fn;
|
|
247
|
+
return id;
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
use(fn) {
|
|
254
|
+
this._fns = [...this._fns, fn];
|
|
255
|
+
return this._fns.length - 1;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
// do not add `Middleware` as return type so we can use _fns internally
|
|
259
|
+
export const createInterceptors = () => ({
|
|
260
|
+
error: new Interceptors(),
|
|
261
|
+
request: new Interceptors(),
|
|
262
|
+
response: new Interceptors(),
|
|
263
|
+
});
|
|
264
|
+
const defaultQuerySerializer = createQuerySerializer({
|
|
265
|
+
allowReserved: false,
|
|
266
|
+
array: {
|
|
267
|
+
explode: true,
|
|
268
|
+
style: 'form',
|
|
269
|
+
},
|
|
270
|
+
object: {
|
|
271
|
+
explode: true,
|
|
272
|
+
style: 'deepObject',
|
|
273
|
+
},
|
|
274
|
+
});
|
|
275
|
+
const defaultHeaders = {
|
|
276
|
+
'Content-Type': 'application/json',
|
|
277
|
+
};
|
|
278
|
+
export const createConfig = (override = {}) => ({
|
|
279
|
+
...jsonBodySerializer,
|
|
280
|
+
headers: defaultHeaders,
|
|
281
|
+
parseAs: 'auto',
|
|
282
|
+
querySerializer: defaultQuerySerializer,
|
|
283
|
+
...override,
|
|
284
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const getAuthToken = async (auth, callback) => {
|
|
2
|
+
const token = typeof callback === 'function' ? await callback(auth) : callback;
|
|
3
|
+
if (!token) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
if (auth.scheme === 'bearer') {
|
|
7
|
+
return `Bearer ${token}`;
|
|
8
|
+
}
|
|
9
|
+
if (auth.scheme === 'basic') {
|
|
10
|
+
return `Basic ${btoa(token)}`;
|
|
11
|
+
}
|
|
12
|
+
return token;
|
|
13
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const serializeFormDataPair = (data, key, value) => {
|
|
2
|
+
if (typeof value === 'string' || value instanceof Blob) {
|
|
3
|
+
data.append(key, value);
|
|
4
|
+
}
|
|
5
|
+
else {
|
|
6
|
+
data.append(key, JSON.stringify(value));
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
const serializeUrlSearchParamsPair = (data, key, value) => {
|
|
10
|
+
if (typeof value === 'string') {
|
|
11
|
+
data.append(key, value);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
data.append(key, JSON.stringify(value));
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
export const formDataBodySerializer = {
|
|
18
|
+
bodySerializer: (body) => {
|
|
19
|
+
const data = new FormData();
|
|
20
|
+
Object.entries(body).forEach(([key, value]) => {
|
|
21
|
+
if (value === undefined || value === null) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (Array.isArray(value)) {
|
|
25
|
+
value.forEach((v) => serializeFormDataPair(data, key, v));
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
serializeFormDataPair(data, key, value);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
return data;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
export const jsonBodySerializer = {
|
|
35
|
+
bodySerializer: (body) => JSON.stringify(body, (_key, value) => typeof value === 'bigint' ? value.toString() : value),
|
|
36
|
+
};
|
|
37
|
+
export const urlSearchParamsBodySerializer = {
|
|
38
|
+
bodySerializer: (body) => {
|
|
39
|
+
const data = new URLSearchParams();
|
|
40
|
+
Object.entries(body).forEach(([key, value]) => {
|
|
41
|
+
if (value === undefined || value === null) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (Array.isArray(value)) {
|
|
45
|
+
value.forEach((v) => serializeUrlSearchParamsPair(data, key, v));
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
serializeUrlSearchParamsPair(data, key, value);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return data.toString();
|
|
52
|
+
},
|
|
53
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
const extraPrefixesMap = {
|
|
2
|
+
$body_: 'body',
|
|
3
|
+
$headers_: 'headers',
|
|
4
|
+
$path_: 'path',
|
|
5
|
+
$query_: 'query',
|
|
6
|
+
};
|
|
7
|
+
const extraPrefixes = Object.entries(extraPrefixesMap);
|
|
8
|
+
const buildKeyMap = (fields, map) => {
|
|
9
|
+
if (!map) {
|
|
10
|
+
map = new Map();
|
|
11
|
+
}
|
|
12
|
+
for (const config of fields) {
|
|
13
|
+
if ('in' in config) {
|
|
14
|
+
if (config.key) {
|
|
15
|
+
map.set(config.key, {
|
|
16
|
+
in: config.in,
|
|
17
|
+
map: config.map,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
else if (config.args) {
|
|
22
|
+
buildKeyMap(config.args, map);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return map;
|
|
26
|
+
};
|
|
27
|
+
const stripEmptySlots = (params) => {
|
|
28
|
+
for (const [slot, value] of Object.entries(params)) {
|
|
29
|
+
if (value && typeof value === 'object' && !Object.keys(value).length) {
|
|
30
|
+
delete params[slot];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
export const buildClientParams = (args, fields) => {
|
|
35
|
+
const params = {
|
|
36
|
+
body: {},
|
|
37
|
+
headers: {},
|
|
38
|
+
path: {},
|
|
39
|
+
query: {},
|
|
40
|
+
};
|
|
41
|
+
const map = buildKeyMap(fields);
|
|
42
|
+
let config;
|
|
43
|
+
for (const [index, arg] of args.entries()) {
|
|
44
|
+
if (fields[index]) {
|
|
45
|
+
config = fields[index];
|
|
46
|
+
}
|
|
47
|
+
if (!config) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if ('in' in config) {
|
|
51
|
+
if (config.key) {
|
|
52
|
+
const field = map.get(config.key);
|
|
53
|
+
const name = field.map || config.key;
|
|
54
|
+
params[field.in][name] = arg;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
params.body = arg;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
for (const [key, value] of Object.entries(arg ?? {})) {
|
|
62
|
+
const field = map.get(key);
|
|
63
|
+
if (field) {
|
|
64
|
+
const name = field.map || key;
|
|
65
|
+
params[field.in][name] = value;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
const extra = extraPrefixes.find(([prefix]) => key.startsWith(prefix));
|
|
69
|
+
if (extra) {
|
|
70
|
+
const [prefix, slot] = extra;
|
|
71
|
+
params[slot][key.slice(prefix.length)] = value;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
for (const [slot, allowed] of Object.entries(config.allowExtra ?? {})) {
|
|
75
|
+
if (allowed) {
|
|
76
|
+
params[slot][key] = value;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
stripEmptySlots(params);
|
|
86
|
+
return params;
|
|
87
|
+
};
|