@swapkit/api-sdk-typescript 1.0.0 → 1.0.5
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.cjs +58 -56
- package/dist/index.d.cts +1500 -1364
- package/dist/index.d.ts +1500 -1364
- package/dist/index.js +49 -47
- package/package.json +5 -5
- package/src/client.ts +16 -0
- package/src/config.ts +16 -0
- package/src/core/auth.ts +41 -0
- package/src/core/bodySerializer.ts +82 -0
- package/src/core/params.ts +169 -0
- package/src/core/pathSerializer.ts +171 -0
- package/src/core/queryKeySerializer.ts +117 -0
- package/src/core/serverSentEvents.ts +242 -0
- package/src/core/types.ts +104 -0
- package/src/core/utils.ts +140 -0
- package/src/http/client.ts +298 -0
- package/src/http/index.ts +25 -0
- package/src/http/types.ts +215 -0
- package/src/http/utils.ts +316 -0
- package/src/index.ts +92 -0
- package/src/sdk.ts +115 -0
- package/src/types.ts +2039 -0
package/dist/index.js
CHANGED
|
@@ -13,7 +13,7 @@ var extraPrefixesMap = {
|
|
|
13
13
|
var extraPrefixes = Object.entries(extraPrefixesMap);
|
|
14
14
|
|
|
15
15
|
// src/core/serverSentEvents.ts
|
|
16
|
-
|
|
16
|
+
function createSseClient({
|
|
17
17
|
onRequest,
|
|
18
18
|
onSseError,
|
|
19
19
|
onSseEvent,
|
|
@@ -25,7 +25,7 @@ var createSseClient = ({
|
|
|
25
25
|
sseSleepFn,
|
|
26
26
|
url,
|
|
27
27
|
...options
|
|
28
|
-
})
|
|
28
|
+
}) {
|
|
29
29
|
let lastEventId;
|
|
30
30
|
const sleep = sseSleepFn ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
31
31
|
const createStream = async function* () {
|
|
@@ -69,7 +69,7 @@ var createSseClient = ({
|
|
|
69
69
|
const { done, value } = await reader.read();
|
|
70
70
|
if (done) break;
|
|
71
71
|
buffer += value;
|
|
72
|
-
buffer = buffer.replace(/\r\n
|
|
72
|
+
buffer = buffer.replace(/\r\n?/g, "\n");
|
|
73
73
|
const chunks = buffer.split("\n\n");
|
|
74
74
|
buffer = chunks.pop() ?? "";
|
|
75
75
|
for (const chunk of chunks) {
|
|
@@ -137,7 +137,7 @@ var createSseClient = ({
|
|
|
137
137
|
};
|
|
138
138
|
const stream = createStream();
|
|
139
139
|
return { stream };
|
|
140
|
-
}
|
|
140
|
+
}
|
|
141
141
|
|
|
142
142
|
// src/core/pathSerializer.ts
|
|
143
143
|
var separatorArrayExplode = (style) => {
|
|
@@ -625,8 +625,9 @@ var createClient = (config = {}) => {
|
|
|
625
625
|
if (opts.body === void 0 || opts.serializedBody === "") {
|
|
626
626
|
opts.headers.delete("Content-Type");
|
|
627
627
|
}
|
|
628
|
-
const
|
|
629
|
-
|
|
628
|
+
const resolvedOpts = opts;
|
|
629
|
+
const url = buildUrl(resolvedOpts);
|
|
630
|
+
return { opts: resolvedOpts, url };
|
|
630
631
|
};
|
|
631
632
|
const request = async (options) => {
|
|
632
633
|
const { opts, url } = await beforeRequest(options);
|
|
@@ -772,8 +773,9 @@ var createClient = (config = {}) => {
|
|
|
772
773
|
url
|
|
773
774
|
});
|
|
774
775
|
};
|
|
776
|
+
const _buildUrl = (options) => buildUrl({ ..._config, ...options });
|
|
775
777
|
return {
|
|
776
|
-
buildUrl,
|
|
778
|
+
buildUrl: _buildUrl,
|
|
777
779
|
connect: makeMethodFn("CONNECT"),
|
|
778
780
|
delete: makeMethodFn("DELETE"),
|
|
779
781
|
get: makeMethodFn("GET"),
|
|
@@ -802,7 +804,7 @@ var createClient = (config = {}) => {
|
|
|
802
804
|
};
|
|
803
805
|
|
|
804
806
|
// src/client.ts
|
|
805
|
-
var client = createClient(createConfig({ baseUrl: "http://localhost:
|
|
807
|
+
var client = createClient(createConfig({ baseUrl: "http://localhost:0" }));
|
|
806
808
|
|
|
807
809
|
// src/config.ts
|
|
808
810
|
function configureSwapKit(options) {
|
|
@@ -824,17 +826,17 @@ var getTokens = (options) => (options?.client ?? client).get({
|
|
|
824
826
|
url: "/tokens",
|
|
825
827
|
...options
|
|
826
828
|
});
|
|
827
|
-
var
|
|
829
|
+
var getSwapToAssets = (options) => (options.client ?? client).get({
|
|
828
830
|
security: [{ name: "x-api-key", type: "apiKey" }],
|
|
829
831
|
url: "/swapTo",
|
|
830
832
|
...options
|
|
831
833
|
});
|
|
832
|
-
var
|
|
834
|
+
var getSwapFromAssets = (options) => (options.client ?? client).get({
|
|
833
835
|
security: [{ name: "x-api-key", type: "apiKey" }],
|
|
834
836
|
url: "/swapFrom",
|
|
835
837
|
...options
|
|
836
838
|
});
|
|
837
|
-
var
|
|
839
|
+
var createBrokerChannel = (options) => (options.client ?? client).post({
|
|
838
840
|
security: [{ name: "x-api-key", type: "apiKey" }],
|
|
839
841
|
url: "/chainflip/broker/channel",
|
|
840
842
|
...options,
|
|
@@ -843,7 +845,7 @@ var postChainflipBrokerChannel = (options) => (options.client ?? client).post({
|
|
|
843
845
|
...options.headers
|
|
844
846
|
}
|
|
845
847
|
});
|
|
846
|
-
var
|
|
848
|
+
var trackTransaction = (options) => (options?.client ?? client).post({
|
|
847
849
|
security: [{ name: "x-api-key", type: "apiKey" }],
|
|
848
850
|
url: "/track",
|
|
849
851
|
...options,
|
|
@@ -852,7 +854,7 @@ var postTrack = (options) => (options?.client ?? client).post({
|
|
|
852
854
|
...options?.headers
|
|
853
855
|
}
|
|
854
856
|
});
|
|
855
|
-
var
|
|
857
|
+
var screenAddress = (options) => (options.client ?? client).post({
|
|
856
858
|
security: [{ name: "x-api-key", type: "apiKey" }],
|
|
857
859
|
url: "/screen",
|
|
858
860
|
...options,
|
|
@@ -861,17 +863,17 @@ var postScreen = (options) => (options.client ?? client).post({
|
|
|
861
863
|
...options.headers
|
|
862
864
|
}
|
|
863
865
|
});
|
|
864
|
-
var
|
|
866
|
+
var getGasPrices = (options) => (options?.client ?? client).get({
|
|
865
867
|
security: [{ name: "x-api-key", type: "apiKey" }],
|
|
866
|
-
url: "/gas
|
|
868
|
+
url: "/gas",
|
|
867
869
|
...options
|
|
868
870
|
});
|
|
869
|
-
var
|
|
871
|
+
var getGasHistory = (options) => (options?.client ?? client).get({
|
|
870
872
|
security: [{ name: "x-api-key", type: "apiKey" }],
|
|
871
|
-
url: "/gas",
|
|
873
|
+
url: "/gas/history",
|
|
872
874
|
...options
|
|
873
875
|
});
|
|
874
|
-
var
|
|
876
|
+
var getCachedPrice = (options) => (options.client ?? client).post({
|
|
875
877
|
security: [{ name: "x-api-key", type: "apiKey" }],
|
|
876
878
|
url: "/price/cached-price",
|
|
877
879
|
...options,
|
|
@@ -880,7 +882,7 @@ var postPriceCachedPrice = (options) => (options.client ?? client).post({
|
|
|
880
882
|
...options.headers
|
|
881
883
|
}
|
|
882
884
|
});
|
|
883
|
-
var
|
|
885
|
+
var getQuote = (options) => (options.client ?? client).post({
|
|
884
886
|
security: [{ name: "x-api-key", type: "apiKey" }],
|
|
885
887
|
url: "/v3/quote",
|
|
886
888
|
...options,
|
|
@@ -889,7 +891,7 @@ var postV3Quote = (options) => (options.client ?? client).post({
|
|
|
889
891
|
...options.headers
|
|
890
892
|
}
|
|
891
893
|
});
|
|
892
|
-
var
|
|
894
|
+
var executeSwap = (options) => (options.client ?? client).post({
|
|
893
895
|
security: [{ name: "x-api-key", type: "apiKey" }],
|
|
894
896
|
url: "/v3/swap",
|
|
895
897
|
...options,
|
|
@@ -904,43 +906,43 @@ var SwapKitService = class {
|
|
|
904
906
|
};
|
|
905
907
|
SwapKitService.getProviders = getProviders;
|
|
906
908
|
SwapKitService.getTokens = getTokens;
|
|
907
|
-
SwapKitService.getSwapTo =
|
|
908
|
-
SwapKitService.getSwapFrom =
|
|
909
|
-
SwapKitService.getQuote =
|
|
910
|
-
SwapKitService.getSwap =
|
|
911
|
-
SwapKitService.trackTransaction =
|
|
912
|
-
SwapKitService.screenAddress =
|
|
913
|
-
SwapKitService.getGasPrices =
|
|
909
|
+
SwapKitService.getSwapTo = getSwapToAssets;
|
|
910
|
+
SwapKitService.getSwapFrom = getSwapFromAssets;
|
|
911
|
+
SwapKitService.getQuote = getQuote;
|
|
912
|
+
SwapKitService.getSwap = executeSwap;
|
|
913
|
+
SwapKitService.trackTransaction = trackTransaction;
|
|
914
|
+
SwapKitService.screenAddress = screenAddress;
|
|
915
|
+
SwapKitService.getGasPrices = getGasPrices;
|
|
914
916
|
SwapKitService.getGasHistory = getGasHistory;
|
|
915
|
-
SwapKitService.getCachedPrice =
|
|
916
|
-
SwapKitService.createBrokerChannel =
|
|
917
|
+
SwapKitService.getCachedPrice = getCachedPrice;
|
|
918
|
+
SwapKitService.createBrokerChannel = createBrokerChannel;
|
|
917
919
|
var TokenlistService = {
|
|
918
920
|
getProviders,
|
|
919
921
|
getTokens,
|
|
920
|
-
getSwapTo,
|
|
921
|
-
getSwapFrom
|
|
922
|
+
getSwapTo: getSwapToAssets,
|
|
923
|
+
getSwapFrom: getSwapFromAssets
|
|
922
924
|
};
|
|
923
925
|
var QuoteService = {
|
|
924
|
-
getQuote
|
|
926
|
+
getQuote
|
|
925
927
|
};
|
|
926
928
|
var SwapService = {
|
|
927
|
-
getSwap:
|
|
929
|
+
getSwap: executeSwap
|
|
928
930
|
};
|
|
929
931
|
var TrackService = {
|
|
930
|
-
trackTransaction
|
|
932
|
+
trackTransaction
|
|
931
933
|
};
|
|
932
934
|
var ScreenService = {
|
|
933
|
-
screenAddress
|
|
935
|
+
screenAddress
|
|
934
936
|
};
|
|
935
937
|
var GasService = {
|
|
936
|
-
getGasPrices
|
|
938
|
+
getGasPrices,
|
|
937
939
|
getGasHistory
|
|
938
940
|
};
|
|
939
941
|
var PriceService = {
|
|
940
|
-
getCachedPrice
|
|
942
|
+
getCachedPrice
|
|
941
943
|
};
|
|
942
944
|
var ChainflipService = {
|
|
943
|
-
createBrokerChannel
|
|
945
|
+
createBrokerChannel
|
|
944
946
|
};
|
|
945
947
|
export {
|
|
946
948
|
ChainflipService,
|
|
@@ -954,16 +956,16 @@ export {
|
|
|
954
956
|
TrackService,
|
|
955
957
|
client,
|
|
956
958
|
configureSwapKit,
|
|
957
|
-
|
|
959
|
+
createBrokerChannel,
|
|
960
|
+
executeSwap,
|
|
961
|
+
getCachedPrice,
|
|
958
962
|
getGasHistory,
|
|
963
|
+
getGasPrices,
|
|
959
964
|
getProviders,
|
|
960
|
-
|
|
961
|
-
|
|
965
|
+
getQuote,
|
|
966
|
+
getSwapFromAssets,
|
|
967
|
+
getSwapToAssets,
|
|
962
968
|
getTokens,
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
postScreen,
|
|
966
|
-
postTrack,
|
|
967
|
-
postV3Quote,
|
|
968
|
-
postV3Swap
|
|
969
|
+
screenAddress,
|
|
970
|
+
trackTransaction
|
|
969
971
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@swapkit/api-sdk-typescript",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Type-safe TypeScript SDK for the SwapKit API",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -12,16 +12,16 @@
|
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
"files": [
|
|
15
|
-
"dist/"
|
|
15
|
+
"dist/",
|
|
16
|
+
"src/"
|
|
16
17
|
],
|
|
17
18
|
"publishConfig": {
|
|
18
19
|
"access": "public"
|
|
19
20
|
},
|
|
20
21
|
"scripts": {
|
|
21
|
-
"clean": "rm -rf dist",
|
|
22
22
|
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
23
|
-
"
|
|
24
|
-
"
|
|
23
|
+
"typecheck": "tsc --noEmit",
|
|
24
|
+
"release:check": "npm run typecheck && npm run build && npm pack --dry-run"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {},
|
|
27
27
|
"devDependencies": {
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// This file is auto-generated by @hey-api/openapi-ts
|
|
2
|
+
|
|
3
|
+
import { type ClientOptions, type Config, createClient, createConfig } from './http';
|
|
4
|
+
import type { ClientOptions as ClientOptions2 } from './types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The `createClientConfig()` function will be called on client initialization
|
|
8
|
+
* and the returned object will become the client's initial configuration.
|
|
9
|
+
*
|
|
10
|
+
* You may want to initialize your client this way instead of calling
|
|
11
|
+
* `setConfig()`. This is useful for example if you're using Next.js
|
|
12
|
+
* to ensure your client always has the correct values.
|
|
13
|
+
*/
|
|
14
|
+
export type CreateClientConfig<T extends ClientOptions = ClientOptions2> = (override?: Config<ClientOptions & T>) => Config<Required<ClientOptions> & T>;
|
|
15
|
+
|
|
16
|
+
export const client = createClient(createConfig<ClientOptions2>({ baseUrl: 'http://localhost:0' }));
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { client } from "./client";
|
|
2
|
+
|
|
3
|
+
export interface SwapKitClientOptions {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function configureSwapKit(options: SwapKitClientOptions) {
|
|
9
|
+
const { apiKey, baseUrl = "https://api.swapkit.dev" } = options;
|
|
10
|
+
client.setConfig({
|
|
11
|
+
baseUrl,
|
|
12
|
+
headers: { "x-api-key": apiKey },
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { client };
|
package/src/core/auth.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// This file is auto-generated by @hey-api/openapi-ts
|
|
2
|
+
|
|
3
|
+
export type AuthToken = string | undefined;
|
|
4
|
+
|
|
5
|
+
export interface Auth {
|
|
6
|
+
/**
|
|
7
|
+
* Which part of the request do we use to send the auth?
|
|
8
|
+
*
|
|
9
|
+
* @default 'header'
|
|
10
|
+
*/
|
|
11
|
+
in?: 'header' | 'query' | 'cookie';
|
|
12
|
+
/**
|
|
13
|
+
* Header or query parameter name.
|
|
14
|
+
*
|
|
15
|
+
* @default 'Authorization'
|
|
16
|
+
*/
|
|
17
|
+
name?: string;
|
|
18
|
+
scheme?: 'basic' | 'bearer';
|
|
19
|
+
type: 'apiKey' | 'http';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const getAuthToken = async (
|
|
23
|
+
auth: Auth,
|
|
24
|
+
callback: ((auth: Auth) => Promise<AuthToken> | AuthToken) | AuthToken,
|
|
25
|
+
): Promise<string | undefined> => {
|
|
26
|
+
const token = typeof callback === 'function' ? await callback(auth) : callback;
|
|
27
|
+
|
|
28
|
+
if (!token) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (auth.scheme === 'bearer') {
|
|
33
|
+
return `Bearer ${token}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (auth.scheme === 'basic') {
|
|
37
|
+
return `Basic ${btoa(token)}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return token;
|
|
41
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// This file is auto-generated by @hey-api/openapi-ts
|
|
2
|
+
|
|
3
|
+
import type { ArrayStyle, ObjectStyle, SerializerOptions } from './pathSerializer';
|
|
4
|
+
|
|
5
|
+
export type QuerySerializer = (query: Record<string, unknown>) => string;
|
|
6
|
+
|
|
7
|
+
export type BodySerializer = (body: unknown) => unknown;
|
|
8
|
+
|
|
9
|
+
type QuerySerializerOptionsObject = {
|
|
10
|
+
allowReserved?: boolean;
|
|
11
|
+
array?: Partial<SerializerOptions<ArrayStyle>>;
|
|
12
|
+
object?: Partial<SerializerOptions<ObjectStyle>>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type QuerySerializerOptions = QuerySerializerOptionsObject & {
|
|
16
|
+
/**
|
|
17
|
+
* Per-parameter serialization overrides. When provided, these settings
|
|
18
|
+
* override the global array/object settings for specific parameter names.
|
|
19
|
+
*/
|
|
20
|
+
parameters?: Record<string, QuerySerializerOptionsObject>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const serializeFormDataPair = (data: FormData, key: string, value: unknown): void => {
|
|
24
|
+
if (typeof value === 'string' || value instanceof Blob) {
|
|
25
|
+
data.append(key, value);
|
|
26
|
+
} else if (value instanceof Date) {
|
|
27
|
+
data.append(key, value.toISOString());
|
|
28
|
+
} else {
|
|
29
|
+
data.append(key, JSON.stringify(value));
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const serializeUrlSearchParamsPair = (data: URLSearchParams, key: string, value: unknown): void => {
|
|
34
|
+
if (typeof value === 'string') {
|
|
35
|
+
data.append(key, value);
|
|
36
|
+
} else {
|
|
37
|
+
data.append(key, JSON.stringify(value));
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const formDataBodySerializer = {
|
|
42
|
+
bodySerializer: (body: unknown): FormData => {
|
|
43
|
+
const data = new FormData();
|
|
44
|
+
|
|
45
|
+
Object.entries(body as Record<string, unknown>).forEach(([key, value]) => {
|
|
46
|
+
if (value === undefined || value === null) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (Array.isArray(value)) {
|
|
50
|
+
value.forEach((v) => serializeFormDataPair(data, key, v));
|
|
51
|
+
} else {
|
|
52
|
+
serializeFormDataPair(data, key, value);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return data;
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const jsonBodySerializer = {
|
|
61
|
+
bodySerializer: (body: unknown): string =>
|
|
62
|
+
JSON.stringify(body, (_key, value) => (typeof value === 'bigint' ? value.toString() : value)),
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const urlSearchParamsBodySerializer = {
|
|
66
|
+
bodySerializer: (body: unknown): string => {
|
|
67
|
+
const data = new URLSearchParams();
|
|
68
|
+
|
|
69
|
+
Object.entries(body as Record<string, unknown>).forEach(([key, value]) => {
|
|
70
|
+
if (value === undefined || value === null) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (Array.isArray(value)) {
|
|
74
|
+
value.forEach((v) => serializeUrlSearchParamsPair(data, key, v));
|
|
75
|
+
} else {
|
|
76
|
+
serializeUrlSearchParamsPair(data, key, value);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return data.toString();
|
|
81
|
+
},
|
|
82
|
+
};
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// This file is auto-generated by @hey-api/openapi-ts
|
|
2
|
+
|
|
3
|
+
type Slot = 'body' | 'headers' | 'path' | 'query';
|
|
4
|
+
|
|
5
|
+
export type Field =
|
|
6
|
+
| {
|
|
7
|
+
in: Exclude<Slot, 'body'>;
|
|
8
|
+
/**
|
|
9
|
+
* Field name. This is the name we want the user to see and use.
|
|
10
|
+
*/
|
|
11
|
+
key: string;
|
|
12
|
+
/**
|
|
13
|
+
* Field mapped name. This is the name we want to use in the request.
|
|
14
|
+
* If omitted, we use the same value as `key`.
|
|
15
|
+
*/
|
|
16
|
+
map?: string;
|
|
17
|
+
}
|
|
18
|
+
| {
|
|
19
|
+
in: Extract<Slot, 'body'>;
|
|
20
|
+
/**
|
|
21
|
+
* Key isn't required for bodies.
|
|
22
|
+
*/
|
|
23
|
+
key?: string;
|
|
24
|
+
map?: string;
|
|
25
|
+
}
|
|
26
|
+
| {
|
|
27
|
+
/**
|
|
28
|
+
* Field name. This is the name we want the user to see and use.
|
|
29
|
+
*/
|
|
30
|
+
key: string;
|
|
31
|
+
/**
|
|
32
|
+
* Field mapped name. This is the name we want to use in the request.
|
|
33
|
+
* If `in` is omitted, `map` aliases `key` to the transport layer.
|
|
34
|
+
*/
|
|
35
|
+
map: Slot;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export interface Fields {
|
|
39
|
+
allowExtra?: Partial<Record<Slot, boolean>>;
|
|
40
|
+
args?: ReadonlyArray<Field>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type FieldsConfig = ReadonlyArray<Field | Fields>;
|
|
44
|
+
|
|
45
|
+
const extraPrefixesMap: Record<string, Slot> = {
|
|
46
|
+
$body_: 'body',
|
|
47
|
+
$headers_: 'headers',
|
|
48
|
+
$path_: 'path',
|
|
49
|
+
$query_: 'query',
|
|
50
|
+
};
|
|
51
|
+
const extraPrefixes = Object.entries(extraPrefixesMap);
|
|
52
|
+
|
|
53
|
+
type KeyMap = Map<
|
|
54
|
+
string,
|
|
55
|
+
| {
|
|
56
|
+
in: Slot;
|
|
57
|
+
map?: string;
|
|
58
|
+
}
|
|
59
|
+
| {
|
|
60
|
+
in?: never;
|
|
61
|
+
map: Slot;
|
|
62
|
+
}
|
|
63
|
+
>;
|
|
64
|
+
|
|
65
|
+
const buildKeyMap = (fields: FieldsConfig, map?: KeyMap): KeyMap => {
|
|
66
|
+
if (!map) {
|
|
67
|
+
map = new Map();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
for (const config of fields) {
|
|
71
|
+
if ('in' in config) {
|
|
72
|
+
if (config.key) {
|
|
73
|
+
map.set(config.key, {
|
|
74
|
+
in: config.in,
|
|
75
|
+
map: config.map,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
} else if ('key' in config) {
|
|
79
|
+
map.set(config.key, {
|
|
80
|
+
map: config.map,
|
|
81
|
+
});
|
|
82
|
+
} else if (config.args) {
|
|
83
|
+
buildKeyMap(config.args, map);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return map;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
interface Params {
|
|
91
|
+
body: unknown;
|
|
92
|
+
headers: Record<string, unknown>;
|
|
93
|
+
path: Record<string, unknown>;
|
|
94
|
+
query: Record<string, unknown>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const stripEmptySlots = (params: Params) => {
|
|
98
|
+
for (const [slot, value] of Object.entries(params)) {
|
|
99
|
+
if (value && typeof value === 'object' && !Array.isArray(value) && !Object.keys(value).length) {
|
|
100
|
+
delete params[slot as Slot];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export const buildClientParams = (args: ReadonlyArray<unknown>, fields: FieldsConfig) => {
|
|
106
|
+
const params: Params = {
|
|
107
|
+
body: {},
|
|
108
|
+
headers: {},
|
|
109
|
+
path: {},
|
|
110
|
+
query: {},
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const map = buildKeyMap(fields);
|
|
114
|
+
|
|
115
|
+
let config: FieldsConfig[number] | undefined;
|
|
116
|
+
|
|
117
|
+
for (const [index, arg] of args.entries()) {
|
|
118
|
+
if (fields[index]) {
|
|
119
|
+
config = fields[index];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (!config) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if ('in' in config) {
|
|
127
|
+
if (config.key) {
|
|
128
|
+
const field = map.get(config.key)!;
|
|
129
|
+
const name = field.map || config.key;
|
|
130
|
+
if (field.in) {
|
|
131
|
+
(params[field.in] as Record<string, unknown>)[name] = arg;
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
134
|
+
params.body = arg;
|
|
135
|
+
}
|
|
136
|
+
} else {
|
|
137
|
+
for (const [key, value] of Object.entries(arg ?? {})) {
|
|
138
|
+
const field = map.get(key);
|
|
139
|
+
|
|
140
|
+
if (field) {
|
|
141
|
+
if (field.in) {
|
|
142
|
+
const name = field.map || key;
|
|
143
|
+
(params[field.in] as Record<string, unknown>)[name] = value;
|
|
144
|
+
} else {
|
|
145
|
+
params[field.map] = value;
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
const extra = extraPrefixes.find(([prefix]) => key.startsWith(prefix));
|
|
149
|
+
|
|
150
|
+
if (extra) {
|
|
151
|
+
const [prefix, slot] = extra;
|
|
152
|
+
(params[slot] as Record<string, unknown>)[key.slice(prefix.length)] = value;
|
|
153
|
+
} else if ('allowExtra' in config && config.allowExtra) {
|
|
154
|
+
for (const [slot, allowed] of Object.entries(config.allowExtra)) {
|
|
155
|
+
if (allowed) {
|
|
156
|
+
(params[slot as Slot] as Record<string, unknown>)[key] = value;
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
stripEmptySlots(params);
|
|
167
|
+
|
|
168
|
+
return params;
|
|
169
|
+
};
|