-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathindex.ts
184 lines (161 loc) · 5.56 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { isFunction } from "../helpers";
import {
ApiClientConfig,
ApiClientExtension,
ApiClientFactory,
ApiClientFactoryParams,
ApiMethods,
ApplyingContextHooks,
CallableContext,
CreateApiClientFn,
ExtensionHookWith,
ExtensionWith,
} from "../types";
import { applyContextToApi } from "./applyContextToApi";
const apiClientFactory = <
ALL_SETTINGS extends ApiClientConfig,
ALL_FUNCTIONS extends ApiMethods
>(
factoryParams: ApiClientFactoryParams<ALL_SETTINGS, ALL_FUNCTIONS>
): ApiClientFactory<any, ALL_FUNCTIONS> => {
const resolveApi = async (api: any, settings: any) => {
if (typeof api === "function") {
return await api(settings);
}
return api ?? {};
};
const createApiClient: CreateApiClientFn<any, ALL_FUNCTIONS> =
async function createApiClient(
this: CallableContext<ALL_FUNCTIONS>,
config,
customApi: Record<string, any> = {}
) {
const rawExtensions: ApiClientExtension<ALL_FUNCTIONS>[] =
this?.middleware?.extensions || [];
const lifecycles = await Promise.all(
rawExtensions
.filter((extension): extension is ExtensionWith<"hooks"> =>
isFunction(extension?.hooks)
)
.map(async ({ hooks }) =>
hooks(this?.middleware?.req, this?.middleware?.res)
)
);
const _config = await lifecycles
.filter((extension): extension is ExtensionHookWith<"beforeCreate"> =>
isFunction(extension?.beforeCreate)
)
.reduce(async (configSoFar, extension) => {
const resolvedConfig = await configSoFar;
return await extension.beforeCreate({
configuration: resolvedConfig,
});
}, Promise.resolve(config));
const settings = (await factoryParams.onCreate)
? await factoryParams.onCreate(_config)
: { config, client: config.client };
settings.config = await lifecycles
.filter((extension): extension is ExtensionHookWith<"afterCreate"> =>
isFunction(extension?.afterCreate)
)
.reduce(async (configSoFar, extension) => {
const resolvedConfig = await configSoFar;
return await extension.afterCreate({ configuration: resolvedConfig });
}, Promise.resolve(settings.config));
const extensionHooks: ApplyingContextHooks = {
before: async (params) => {
return await lifecycles
.filter((extension): extension is ExtensionHookWith<"beforeCall"> =>
isFunction(extension?.beforeCall)
)
.reduce(async (argsSoFar, extension) => {
const resolvedArgs = await argsSoFar;
const resolvedSettings = await settings;
return extension.beforeCall({
...params,
configuration: resolvedSettings.config,
args: resolvedArgs,
});
}, Promise.resolve(params.args));
},
after: async (params) => {
return await lifecycles
.filter((extension): extension is ExtensionHookWith<"afterCall"> =>
isFunction(extension.afterCall)
)
.reduce(async (responseSoFar, extension) => {
const resolvedResponse = await responseSoFar;
const resolvedSettings = await settings;
return extension.afterCall({
...params,
configuration: resolvedSettings.config,
response: resolvedResponse,
});
}, Promise.resolve(params.response));
},
};
const context = { ...settings, ...(this?.middleware || {}) };
const api = await resolveApi(factoryParams.api, settings);
const namespacedExtensions: Record<string, any> = {};
let sharedExtensions = customApi;
for await (const extension of rawExtensions) {
const extendedApiMethods = await resolveApi(
extension.extendApiMethods,
settings
);
if (extension.isNamespaced) {
namespacedExtensions[extension.name] = {
...(namespacedExtensions?.[extension.name] ?? {}),
...extendedApiMethods,
};
} else {
sharedExtensions = {
...sharedExtensions,
...extendedApiMethods,
};
}
}
const integrationApi = applyContextToApi(
api,
// @ts-expect-error see above
context,
extensionHooks
);
const sharedExtensionsApi = applyContextToApi(
sharedExtensions,
// @ts-expect-error see above
context,
extensionHooks
);
const namespacedApi = {};
for (const [namespace, extension] of Object.entries(
namespacedExtensions
)) {
namespacedApi[namespace] = applyContextToApi(
extension,
// @ts-expect-error see above
context,
extensionHooks
);
}
const extendedApi = {
...sharedExtensionsApi,
...namespacedApi,
};
const mergedApi = {
...integrationApi,
...extendedApi,
} as ALL_FUNCTIONS;
// api methods haven't been invoked yet, so we still have time to add them to the context
(context as any).api = integrationApi;
(context as any).extendedApi = extendedApi;
return {
api: mergedApi,
client: settings.client,
settings: settings.config,
};
};
createApiClient._predefinedExtensions = factoryParams.extensions || [];
return { createApiClient, init: factoryParams.init };
};
export { apiClientFactory };