@tahanabavi/typefetch 1.2.2 → 1.3.0
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/README.md +228 -87
- package/dist/index.d.mts +29 -18
- package/dist/index.d.ts +29 -18
- package/dist/index.js +95 -22
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +94 -22
- package/dist/index.mjs.map +1 -1
- package/package.json +23 -3
package/dist/index.js
CHANGED
|
@@ -20,6 +20,18 @@ var __spreadValues = (a, b) => {
|
|
|
20
20
|
return a;
|
|
21
21
|
};
|
|
22
22
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
23
|
+
var __objRest = (source, exclude) => {
|
|
24
|
+
var target = {};
|
|
25
|
+
for (var prop in source)
|
|
26
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
27
|
+
target[prop] = source[prop];
|
|
28
|
+
if (source != null && __getOwnPropSymbols)
|
|
29
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
30
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
31
|
+
target[prop] = source[prop];
|
|
32
|
+
}
|
|
33
|
+
return target;
|
|
34
|
+
};
|
|
23
35
|
var __export = (target, all) => {
|
|
24
36
|
for (var name in all)
|
|
25
37
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -62,6 +74,7 @@ __export(index_exports, {
|
|
|
62
74
|
authMiddleware: () => authMiddleware,
|
|
63
75
|
cacheMiddleware: () => cacheMiddleware,
|
|
64
76
|
loggingMiddleware: () => loggingMiddleware,
|
|
77
|
+
makeRequestSchema: () => makeRequestSchema,
|
|
65
78
|
retryMiddleware: () => retryMiddleware
|
|
66
79
|
});
|
|
67
80
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -4119,15 +4132,19 @@ var ApiClient = class {
|
|
|
4119
4132
|
* path: z.object({...}).optional(),
|
|
4120
4133
|
* query: z.object({...}).optional(),
|
|
4121
4134
|
* body: z.any().optional(),
|
|
4135
|
+
* header: z.object({...}).optional(),
|
|
4122
4136
|
* })
|
|
4123
4137
|
*
|
|
4124
|
-
* If the parsed request does not contain `path`, `query` or `body`,
|
|
4138
|
+
* If the parsed request does not contain `path`, `header`,`query` or `body`,
|
|
4125
4139
|
* the entire input is treated as the legacy flat request body.
|
|
4126
4140
|
*/
|
|
4127
4141
|
request(endpoint, input) {
|
|
4128
4142
|
return __async(this, null, function* () {
|
|
4129
|
-
var
|
|
4143
|
+
var _b, _c, _d, _e;
|
|
4130
4144
|
const parsedInput = endpoint.request.parse(input);
|
|
4145
|
+
const isObject = typeof parsedInput === "object" && parsedInput !== null;
|
|
4146
|
+
const looksStructured = isObject && ("path" in parsedInput || "query" in parsedInput || "body" in parsedInput || "headers" in parsedInput);
|
|
4147
|
+
const _a = parsedInput, { headers: extraHeaders } = _a, restInput = __objRest(_a, ["headers"]);
|
|
4131
4148
|
if (this.useMockData && endpoint.mockData) {
|
|
4132
4149
|
return this.handleMockRequest(endpoint);
|
|
4133
4150
|
}
|
|
@@ -4141,14 +4158,32 @@ var ApiClient = class {
|
|
|
4141
4158
|
status: 401,
|
|
4142
4159
|
code: "NO_TOKEN"
|
|
4143
4160
|
});
|
|
4144
|
-
(
|
|
4161
|
+
(_b = this.errorHandler) == null ? void 0 : _b.call(this, error);
|
|
4145
4162
|
throw error;
|
|
4146
4163
|
}
|
|
4147
|
-
const headers = {
|
|
4164
|
+
const headers = {};
|
|
4148
4165
|
if (endpoint.auth && token) {
|
|
4149
4166
|
headers["Authorization"] = `Bearer ${token}`;
|
|
4150
4167
|
}
|
|
4151
|
-
|
|
4168
|
+
if (endpoint.bodyType !== "form-data") {
|
|
4169
|
+
headers["Content-Type"] = "application/json";
|
|
4170
|
+
}
|
|
4171
|
+
if (endpoint.headers) {
|
|
4172
|
+
const endpointHeaders = typeof endpoint.headers === "function" ? endpoint.headers(parsedInput) : endpoint.headers;
|
|
4173
|
+
for (const [key, value] of Object.entries(endpointHeaders)) {
|
|
4174
|
+
headers[key] = value;
|
|
4175
|
+
}
|
|
4176
|
+
}
|
|
4177
|
+
if (extraHeaders) {
|
|
4178
|
+
for (const [key, value] of Object.entries(extraHeaders)) {
|
|
4179
|
+
headers[key] = value;
|
|
4180
|
+
}
|
|
4181
|
+
}
|
|
4182
|
+
const { url, body } = this.buildUrlAndBody(
|
|
4183
|
+
endpoint,
|
|
4184
|
+
restInput,
|
|
4185
|
+
looksStructured
|
|
4186
|
+
);
|
|
4152
4187
|
const ctx = {
|
|
4153
4188
|
url,
|
|
4154
4189
|
init: {
|
|
@@ -4174,7 +4209,7 @@ var ApiClient = class {
|
|
|
4174
4209
|
status: parsedResponse.code || res.status,
|
|
4175
4210
|
code: `API_ERROR_${parsedResponse.code}`
|
|
4176
4211
|
});
|
|
4177
|
-
(
|
|
4212
|
+
(_c = this.errorHandler) == null ? void 0 : _c.call(this, error);
|
|
4178
4213
|
throw error;
|
|
4179
4214
|
}
|
|
4180
4215
|
responseData = parsedResponse.data;
|
|
@@ -4188,34 +4223,44 @@ var ApiClient = class {
|
|
|
4188
4223
|
detail: json.detail,
|
|
4189
4224
|
errors: json.errors
|
|
4190
4225
|
});
|
|
4191
|
-
(
|
|
4226
|
+
(_d = this.errorHandler) == null ? void 0 : _d.call(this, error);
|
|
4192
4227
|
throw error;
|
|
4193
4228
|
}
|
|
4194
4229
|
return this.responseTransform(endpoint.response.parse(responseData));
|
|
4195
4230
|
} catch (err) {
|
|
4196
4231
|
const error = this.normalizeError(err);
|
|
4197
|
-
(
|
|
4232
|
+
(_e = this.errorHandler) == null ? void 0 : _e.call(this, error);
|
|
4198
4233
|
throw error;
|
|
4199
4234
|
}
|
|
4200
4235
|
});
|
|
4201
4236
|
}
|
|
4202
4237
|
/**
|
|
4203
|
-
* Builds
|
|
4204
|
-
*
|
|
4205
|
-
* - Legacy mode: if the input does not contain `path`, `query` or `body`,
|
|
4206
|
-
* the entire input is used as the JSON request body for non-GET methods.
|
|
4207
|
-
*
|
|
4208
|
-
* - New mode: uses `path` to interpolate `:param` segments, `query` to
|
|
4209
|
-
* construct the query string, and `body` as the JSON payload.
|
|
4238
|
+
* Builds final URL and body from endpoint + request input.
|
|
4239
|
+
* Supports both structured `{ path, query, body, headers }` and legacy flat input.
|
|
4210
4240
|
*/
|
|
4211
|
-
buildUrlAndBody(endpoint, parsedInput) {
|
|
4212
|
-
|
|
4213
|
-
const hasNewShape = isObject && ("path" in parsedInput || "query" in parsedInput || "body" in parsedInput);
|
|
4214
|
-
if (!hasNewShape) {
|
|
4241
|
+
buildUrlAndBody(endpoint, parsedInput, looksStructured) {
|
|
4242
|
+
if (!looksStructured) {
|
|
4215
4243
|
const url2 = this.config.baseUrl + endpoint.path;
|
|
4216
4244
|
let requestBody2 = void 0;
|
|
4217
4245
|
if (endpoint.method !== "GET") {
|
|
4218
|
-
|
|
4246
|
+
if (endpoint.bodyType === "form-data") {
|
|
4247
|
+
const form = new FormData();
|
|
4248
|
+
const flat = parsedInput || {};
|
|
4249
|
+
for (const [key, value] of Object.entries(flat)) {
|
|
4250
|
+
if (value === void 0 || value === null) continue;
|
|
4251
|
+
if (Array.isArray(value)) {
|
|
4252
|
+
for (const v of value) {
|
|
4253
|
+
if (v === void 0 || v === null) continue;
|
|
4254
|
+
form.append(key, v);
|
|
4255
|
+
}
|
|
4256
|
+
} else {
|
|
4257
|
+
form.append(key, value);
|
|
4258
|
+
}
|
|
4259
|
+
}
|
|
4260
|
+
requestBody2 = form;
|
|
4261
|
+
} else {
|
|
4262
|
+
requestBody2 = JSON.stringify(parsedInput);
|
|
4263
|
+
}
|
|
4219
4264
|
}
|
|
4220
4265
|
return { url: url2, body: requestBody2 };
|
|
4221
4266
|
}
|
|
@@ -4268,8 +4313,24 @@ var ApiClient = class {
|
|
|
4268
4313
|
}
|
|
4269
4314
|
let requestBody = void 0;
|
|
4270
4315
|
if (endpoint.method !== "GET") {
|
|
4271
|
-
if (
|
|
4272
|
-
|
|
4316
|
+
if (body !== void 0 && body !== null) {
|
|
4317
|
+
if (endpoint.bodyType === "form-data") {
|
|
4318
|
+
const form = new FormData();
|
|
4319
|
+
for (const [key, value] of Object.entries(body)) {
|
|
4320
|
+
if (value === void 0 || value === null) continue;
|
|
4321
|
+
if (Array.isArray(value)) {
|
|
4322
|
+
for (const v of value) {
|
|
4323
|
+
if (v === void 0 || v === null) continue;
|
|
4324
|
+
form.append(key, v);
|
|
4325
|
+
}
|
|
4326
|
+
} else {
|
|
4327
|
+
form.append(key, value);
|
|
4328
|
+
}
|
|
4329
|
+
}
|
|
4330
|
+
requestBody = form;
|
|
4331
|
+
} else {
|
|
4332
|
+
requestBody = JSON.stringify(body);
|
|
4333
|
+
}
|
|
4273
4334
|
}
|
|
4274
4335
|
}
|
|
4275
4336
|
return { url, body: requestBody };
|
|
@@ -4393,6 +4454,17 @@ var cacheMiddleware = (options = {}) => {
|
|
|
4393
4454
|
return next();
|
|
4394
4455
|
});
|
|
4395
4456
|
};
|
|
4457
|
+
|
|
4458
|
+
// src/utils/make-request-schema.ts
|
|
4459
|
+
var makeRequestSchema = () => (defs) => {
|
|
4460
|
+
var _a, _b, _c, _d, _e, _f;
|
|
4461
|
+
return external_exports.object({
|
|
4462
|
+
path: (_b = (_a = defs.path) != null ? _a : external_exports.object({})) == null ? void 0 : _b.optional(),
|
|
4463
|
+
query: (_d = (_c = defs.query) != null ? _c : external_exports.object({})) == null ? void 0 : _d.optional(),
|
|
4464
|
+
body: (_e = defs.body) != null ? _e : external_exports.undefined(),
|
|
4465
|
+
headers: (_f = defs.headers) != null ? _f : external_exports.record(external_exports.string()).optional()
|
|
4466
|
+
});
|
|
4467
|
+
};
|
|
4396
4468
|
// Annotate the CommonJS export names for ESM import in node:
|
|
4397
4469
|
0 && (module.exports = {
|
|
4398
4470
|
ApiClient,
|
|
@@ -4400,6 +4472,7 @@ var cacheMiddleware = (options = {}) => {
|
|
|
4400
4472
|
authMiddleware,
|
|
4401
4473
|
cacheMiddleware,
|
|
4402
4474
|
loggingMiddleware,
|
|
4475
|
+
makeRequestSchema,
|
|
4403
4476
|
retryMiddleware
|
|
4404
4477
|
});
|
|
4405
4478
|
//# sourceMappingURL=index.js.map
|