@vahidkaargar/customized-api-client 0.2.4 → 0.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 +27 -4
- package/dist/index.cjs +75 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +29 -19
- package/dist/index.d.ts +29 -19
- package/dist/index.js +70 -13
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -124,6 +124,9 @@ interface MultiStatusBody {
|
|
|
124
124
|
readonly headers: NormalizedResponseHeaders;
|
|
125
125
|
}
|
|
126
126
|
type ClientSuccess = JsonApiSuccessBody | NoContentBody | AcceptedBody | MultiStatusBody;
|
|
127
|
+
type ClientSuccessWithDocument<T extends JsonApiDocument = JsonApiDocument> = (JsonApiSuccessBody & {
|
|
128
|
+
readonly document: T;
|
|
129
|
+
}) | NoContentBody | AcceptedBody | MultiStatusBody;
|
|
127
130
|
interface OkResult<T extends ClientSuccess> {
|
|
128
131
|
readonly ok: true;
|
|
129
132
|
readonly value: T;
|
|
@@ -154,30 +157,37 @@ interface RequestCallOptions {
|
|
|
154
157
|
readonly signal?: AbortSignal;
|
|
155
158
|
}
|
|
156
159
|
interface ApiClient {
|
|
157
|
-
readonly get: (path: string, opts?: RequestCallOptions) => Promise<
|
|
158
|
-
readonly head: (path: string, opts?: RequestCallOptions) => Promise<
|
|
159
|
-
readonly post: (path: string, data?: unknown, opts?: RequestCallOptions) => Promise<
|
|
160
|
-
readonly patch: (path: string, data?: unknown, opts?: RequestCallOptions) => Promise<
|
|
161
|
-
readonly put: (path: string, data?: unknown, opts?: RequestCallOptions) => Promise<
|
|
162
|
-
readonly delete: (path: string, opts?: RequestCallOptions) => Promise<
|
|
163
|
-
readonly
|
|
164
|
-
readonly
|
|
165
|
-
readonly
|
|
166
|
-
readonly
|
|
167
|
-
readonly
|
|
168
|
-
readonly
|
|
169
|
-
readonly
|
|
170
|
-
readonly
|
|
171
|
-
readonly
|
|
172
|
-
readonly
|
|
173
|
-
readonly
|
|
174
|
-
readonly
|
|
160
|
+
readonly get: <T extends JsonApiDocument = JsonApiDocument>(path: string, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
161
|
+
readonly head: <T extends JsonApiDocument = JsonApiDocument>(path: string, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
162
|
+
readonly post: <T extends JsonApiDocument = JsonApiDocument>(path: string, data?: unknown, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
163
|
+
readonly patch: <T extends JsonApiDocument = JsonApiDocument>(path: string, data?: unknown, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
164
|
+
readonly put: <T extends JsonApiDocument = JsonApiDocument>(path: string, data?: unknown, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
165
|
+
readonly delete: <T extends JsonApiDocument = JsonApiDocument>(path: string, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
166
|
+
readonly postFormData: <T extends JsonApiDocument = JsonApiDocument>(path: string, data: FormData, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
167
|
+
readonly request: <T extends JsonApiDocument = JsonApiDocument>(ax: AxiosRequestConfig, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
168
|
+
readonly getByUrl: <T extends JsonApiDocument = JsonApiDocument>(fullUrl: string, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
169
|
+
readonly patchWithVersion: <T extends JsonApiDocument = JsonApiDocument>(path: string, data: unknown, version: number, opts?: Omit<RequestCallOptions, 'ifMatchVersion'>) => Promise<ClientSuccessWithDocument<T>>;
|
|
170
|
+
readonly safeGet: <T extends JsonApiDocument = JsonApiDocument>(path: string, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
171
|
+
readonly safePost: <T extends JsonApiDocument = JsonApiDocument>(path: string, data?: unknown, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
172
|
+
readonly safePatch: <T extends JsonApiDocument = JsonApiDocument>(path: string, data?: unknown, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
173
|
+
readonly safePut: <T extends JsonApiDocument = JsonApiDocument>(path: string, data?: unknown, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
174
|
+
readonly safeDelete: <T extends JsonApiDocument = JsonApiDocument>(path: string, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
175
|
+
readonly safeHead: <T extends JsonApiDocument = JsonApiDocument>(path: string, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
176
|
+
readonly safeRequest: <T extends JsonApiDocument = JsonApiDocument>(ax: AxiosRequestConfig, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
177
|
+
readonly safeGetByUrl: <T extends JsonApiDocument = JsonApiDocument>(fullUrl: string, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
178
|
+
readonly safePatchWithVersion: <T extends JsonApiDocument = JsonApiDocument>(path: string, data: unknown, version: number, opts?: Omit<RequestCallOptions, 'ifMatchVersion'>) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
175
179
|
}
|
|
176
180
|
declare function createApiClient(config: ApiClientConfig): ApiClient;
|
|
177
181
|
|
|
182
|
+
declare function hasErrorCode(error: unknown, code: string): boolean;
|
|
183
|
+
declare function isApiClientErrorWithCode(error: unknown, code: string): error is ApiClientError;
|
|
178
184
|
declare function isAuthenticationError(e: unknown): boolean;
|
|
179
185
|
declare function isForbiddenError(e: unknown): boolean;
|
|
186
|
+
/** True for any HTTP 428. Prefer code-specific helpers when branching on `errors[].code`. */
|
|
180
187
|
declare function isPreconditionRequiredError(e: unknown): boolean;
|
|
188
|
+
declare function isIdempotencyKeyRequiredError(e: unknown): boolean;
|
|
189
|
+
declare function isIfMatchRequiredError(e: unknown): boolean;
|
|
190
|
+
declare function isMfaVerificationRequiredError(e: unknown): boolean;
|
|
181
191
|
declare function isPreconditionFailedError(e: unknown): boolean;
|
|
182
192
|
declare function isValidationError(e: unknown): boolean;
|
|
183
193
|
declare function isConflictError(e: unknown): boolean;
|
|
@@ -326,4 +336,4 @@ declare function pollAsyncResult(client: ApiClient, initial: Extract<ClientSucce
|
|
|
326
336
|
|
|
327
337
|
declare const PACKAGE_VERSION: string;
|
|
328
338
|
|
|
329
|
-
export { type AcceptedBody, type ApiClient, type ApiClientConfig, ApiClientError, type AuthConfig, type BaseUrlMode, type ClientSuccess, type CursorPagination, DEFAULT_PAGE_SIZE_CAP, DEFAULT_TIMEOUT_MS, type DeprecationInfo, type ErrResult, IDEMPOTENCY_MAX_LENGTH, type IdempotencyReplayContext, type IncludedIndex, type JsonApiDocument, type JsonApiErrorDocument, type JsonApiErrorObject, type JsonApiPrimaryData, type JsonApiQueryInput, type JsonApiResourceLinkage, type JsonApiResourceObject, type JsonApiSuccessBody, type MultiStatusBody, type MultiStatusItem, type NoContentBody, type NormalizedResponseHeaders, type OffsetPagination, type OkResult, PACKAGE_VERSION, type PollOptions, type RequestCallOptions, type Result, type RetryOptions, type TokenProvider, type TransformResponseKeysMode, type UnknownPagination, type ValidationGroups, applyJsonApiHeaders, applyTransformKeys, assertValidIdempotencyKey, buildCursorPageParams, buildJsonApiQuery, buildOffsetPageParams, createApiClient, createHealthCheck, defaultIdempotencyKey, dispatchWithRetry, etagFromResponseHeaders, flattenAxiosHeaders, formatIfMatch, getHeader, getNextPageUrl, groupValidationErrorsByPointer, indexIncluded, isApiClientError, isAuthenticationError, isConflictError, isForbiddenError, isMutationMethod, isPayloadTooLargeError, isPreconditionFailedError, isPreconditionRequiredError, isRetryablePerPolicy, isValidationError, normalizeAxiosResponse, normalizeHttpUrl, parseDeprecationHeaders, parseJsonApiDocument, parseJsonApiErrorBody, parseMultiStatusBody, parsePaginationKind, parseRetryAfterSeconds, pollAsyncResult, readResourceVersion, redactHeaderRecord, resolveAcceptLanguage, resolveAcceptedLocation, resolveAuthorizationHeader, resolveIncluded, resolveResourcePath, retryAllowed, truncateForLog };
|
|
339
|
+
export { type AcceptedBody, type ApiClient, type ApiClientConfig, ApiClientError, type AuthConfig, type BaseUrlMode, type ClientSuccess, type ClientSuccessWithDocument, type CursorPagination, DEFAULT_PAGE_SIZE_CAP, DEFAULT_TIMEOUT_MS, type DeprecationInfo, type ErrResult, IDEMPOTENCY_MAX_LENGTH, type IdempotencyReplayContext, type IncludedIndex, type JsonApiDocument, type JsonApiErrorDocument, type JsonApiErrorObject, type JsonApiPrimaryData, type JsonApiQueryInput, type JsonApiResourceLinkage, type JsonApiResourceObject, type JsonApiSuccessBody, type MultiStatusBody, type MultiStatusItem, type NoContentBody, type NormalizedResponseHeaders, type OffsetPagination, type OkResult, PACKAGE_VERSION, type PollOptions, type RequestCallOptions, type Result, type RetryOptions, type TokenProvider, type TransformResponseKeysMode, type UnknownPagination, type ValidationGroups, applyJsonApiHeaders, applyTransformKeys, assertValidIdempotencyKey, buildCursorPageParams, buildJsonApiQuery, buildOffsetPageParams, createApiClient, createHealthCheck, defaultIdempotencyKey, dispatchWithRetry, etagFromResponseHeaders, flattenAxiosHeaders, formatIfMatch, getHeader, getNextPageUrl, groupValidationErrorsByPointer, hasErrorCode, indexIncluded, isApiClientError, isApiClientErrorWithCode, isAuthenticationError, isConflictError, isForbiddenError, isIdempotencyKeyRequiredError, isIfMatchRequiredError, isMfaVerificationRequiredError, isMutationMethod, isPayloadTooLargeError, isPreconditionFailedError, isPreconditionRequiredError, isRetryablePerPolicy, isValidationError, normalizeAxiosResponse, normalizeHttpUrl, parseDeprecationHeaders, parseJsonApiDocument, parseJsonApiErrorBody, parseMultiStatusBody, parsePaginationKind, parseRetryAfterSeconds, pollAsyncResult, readResourceVersion, redactHeaderRecord, resolveAcceptLanguage, resolveAcceptedLocation, resolveAuthorizationHeader, resolveIncluded, resolveResourcePath, retryAllowed, truncateForLog };
|
package/dist/index.d.ts
CHANGED
|
@@ -124,6 +124,9 @@ interface MultiStatusBody {
|
|
|
124
124
|
readonly headers: NormalizedResponseHeaders;
|
|
125
125
|
}
|
|
126
126
|
type ClientSuccess = JsonApiSuccessBody | NoContentBody | AcceptedBody | MultiStatusBody;
|
|
127
|
+
type ClientSuccessWithDocument<T extends JsonApiDocument = JsonApiDocument> = (JsonApiSuccessBody & {
|
|
128
|
+
readonly document: T;
|
|
129
|
+
}) | NoContentBody | AcceptedBody | MultiStatusBody;
|
|
127
130
|
interface OkResult<T extends ClientSuccess> {
|
|
128
131
|
readonly ok: true;
|
|
129
132
|
readonly value: T;
|
|
@@ -154,30 +157,37 @@ interface RequestCallOptions {
|
|
|
154
157
|
readonly signal?: AbortSignal;
|
|
155
158
|
}
|
|
156
159
|
interface ApiClient {
|
|
157
|
-
readonly get: (path: string, opts?: RequestCallOptions) => Promise<
|
|
158
|
-
readonly head: (path: string, opts?: RequestCallOptions) => Promise<
|
|
159
|
-
readonly post: (path: string, data?: unknown, opts?: RequestCallOptions) => Promise<
|
|
160
|
-
readonly patch: (path: string, data?: unknown, opts?: RequestCallOptions) => Promise<
|
|
161
|
-
readonly put: (path: string, data?: unknown, opts?: RequestCallOptions) => Promise<
|
|
162
|
-
readonly delete: (path: string, opts?: RequestCallOptions) => Promise<
|
|
163
|
-
readonly
|
|
164
|
-
readonly
|
|
165
|
-
readonly
|
|
166
|
-
readonly
|
|
167
|
-
readonly
|
|
168
|
-
readonly
|
|
169
|
-
readonly
|
|
170
|
-
readonly
|
|
171
|
-
readonly
|
|
172
|
-
readonly
|
|
173
|
-
readonly
|
|
174
|
-
readonly
|
|
160
|
+
readonly get: <T extends JsonApiDocument = JsonApiDocument>(path: string, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
161
|
+
readonly head: <T extends JsonApiDocument = JsonApiDocument>(path: string, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
162
|
+
readonly post: <T extends JsonApiDocument = JsonApiDocument>(path: string, data?: unknown, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
163
|
+
readonly patch: <T extends JsonApiDocument = JsonApiDocument>(path: string, data?: unknown, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
164
|
+
readonly put: <T extends JsonApiDocument = JsonApiDocument>(path: string, data?: unknown, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
165
|
+
readonly delete: <T extends JsonApiDocument = JsonApiDocument>(path: string, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
166
|
+
readonly postFormData: <T extends JsonApiDocument = JsonApiDocument>(path: string, data: FormData, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
167
|
+
readonly request: <T extends JsonApiDocument = JsonApiDocument>(ax: AxiosRequestConfig, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
168
|
+
readonly getByUrl: <T extends JsonApiDocument = JsonApiDocument>(fullUrl: string, opts?: RequestCallOptions) => Promise<ClientSuccessWithDocument<T>>;
|
|
169
|
+
readonly patchWithVersion: <T extends JsonApiDocument = JsonApiDocument>(path: string, data: unknown, version: number, opts?: Omit<RequestCallOptions, 'ifMatchVersion'>) => Promise<ClientSuccessWithDocument<T>>;
|
|
170
|
+
readonly safeGet: <T extends JsonApiDocument = JsonApiDocument>(path: string, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
171
|
+
readonly safePost: <T extends JsonApiDocument = JsonApiDocument>(path: string, data?: unknown, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
172
|
+
readonly safePatch: <T extends JsonApiDocument = JsonApiDocument>(path: string, data?: unknown, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
173
|
+
readonly safePut: <T extends JsonApiDocument = JsonApiDocument>(path: string, data?: unknown, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
174
|
+
readonly safeDelete: <T extends JsonApiDocument = JsonApiDocument>(path: string, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
175
|
+
readonly safeHead: <T extends JsonApiDocument = JsonApiDocument>(path: string, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
176
|
+
readonly safeRequest: <T extends JsonApiDocument = JsonApiDocument>(ax: AxiosRequestConfig, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
177
|
+
readonly safeGetByUrl: <T extends JsonApiDocument = JsonApiDocument>(fullUrl: string, opts?: RequestCallOptions) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
178
|
+
readonly safePatchWithVersion: <T extends JsonApiDocument = JsonApiDocument>(path: string, data: unknown, version: number, opts?: Omit<RequestCallOptions, 'ifMatchVersion'>) => Promise<Result<ClientSuccessWithDocument<T>, ApiClientError>>;
|
|
175
179
|
}
|
|
176
180
|
declare function createApiClient(config: ApiClientConfig): ApiClient;
|
|
177
181
|
|
|
182
|
+
declare function hasErrorCode(error: unknown, code: string): boolean;
|
|
183
|
+
declare function isApiClientErrorWithCode(error: unknown, code: string): error is ApiClientError;
|
|
178
184
|
declare function isAuthenticationError(e: unknown): boolean;
|
|
179
185
|
declare function isForbiddenError(e: unknown): boolean;
|
|
186
|
+
/** True for any HTTP 428. Prefer code-specific helpers when branching on `errors[].code`. */
|
|
180
187
|
declare function isPreconditionRequiredError(e: unknown): boolean;
|
|
188
|
+
declare function isIdempotencyKeyRequiredError(e: unknown): boolean;
|
|
189
|
+
declare function isIfMatchRequiredError(e: unknown): boolean;
|
|
190
|
+
declare function isMfaVerificationRequiredError(e: unknown): boolean;
|
|
181
191
|
declare function isPreconditionFailedError(e: unknown): boolean;
|
|
182
192
|
declare function isValidationError(e: unknown): boolean;
|
|
183
193
|
declare function isConflictError(e: unknown): boolean;
|
|
@@ -326,4 +336,4 @@ declare function pollAsyncResult(client: ApiClient, initial: Extract<ClientSucce
|
|
|
326
336
|
|
|
327
337
|
declare const PACKAGE_VERSION: string;
|
|
328
338
|
|
|
329
|
-
export { type AcceptedBody, type ApiClient, type ApiClientConfig, ApiClientError, type AuthConfig, type BaseUrlMode, type ClientSuccess, type CursorPagination, DEFAULT_PAGE_SIZE_CAP, DEFAULT_TIMEOUT_MS, type DeprecationInfo, type ErrResult, IDEMPOTENCY_MAX_LENGTH, type IdempotencyReplayContext, type IncludedIndex, type JsonApiDocument, type JsonApiErrorDocument, type JsonApiErrorObject, type JsonApiPrimaryData, type JsonApiQueryInput, type JsonApiResourceLinkage, type JsonApiResourceObject, type JsonApiSuccessBody, type MultiStatusBody, type MultiStatusItem, type NoContentBody, type NormalizedResponseHeaders, type OffsetPagination, type OkResult, PACKAGE_VERSION, type PollOptions, type RequestCallOptions, type Result, type RetryOptions, type TokenProvider, type TransformResponseKeysMode, type UnknownPagination, type ValidationGroups, applyJsonApiHeaders, applyTransformKeys, assertValidIdempotencyKey, buildCursorPageParams, buildJsonApiQuery, buildOffsetPageParams, createApiClient, createHealthCheck, defaultIdempotencyKey, dispatchWithRetry, etagFromResponseHeaders, flattenAxiosHeaders, formatIfMatch, getHeader, getNextPageUrl, groupValidationErrorsByPointer, indexIncluded, isApiClientError, isAuthenticationError, isConflictError, isForbiddenError, isMutationMethod, isPayloadTooLargeError, isPreconditionFailedError, isPreconditionRequiredError, isRetryablePerPolicy, isValidationError, normalizeAxiosResponse, normalizeHttpUrl, parseDeprecationHeaders, parseJsonApiDocument, parseJsonApiErrorBody, parseMultiStatusBody, parsePaginationKind, parseRetryAfterSeconds, pollAsyncResult, readResourceVersion, redactHeaderRecord, resolveAcceptLanguage, resolveAcceptedLocation, resolveAuthorizationHeader, resolveIncluded, resolveResourcePath, retryAllowed, truncateForLog };
|
|
339
|
+
export { type AcceptedBody, type ApiClient, type ApiClientConfig, ApiClientError, type AuthConfig, type BaseUrlMode, type ClientSuccess, type ClientSuccessWithDocument, type CursorPagination, DEFAULT_PAGE_SIZE_CAP, DEFAULT_TIMEOUT_MS, type DeprecationInfo, type ErrResult, IDEMPOTENCY_MAX_LENGTH, type IdempotencyReplayContext, type IncludedIndex, type JsonApiDocument, type JsonApiErrorDocument, type JsonApiErrorObject, type JsonApiPrimaryData, type JsonApiQueryInput, type JsonApiResourceLinkage, type JsonApiResourceObject, type JsonApiSuccessBody, type MultiStatusBody, type MultiStatusItem, type NoContentBody, type NormalizedResponseHeaders, type OffsetPagination, type OkResult, PACKAGE_VERSION, type PollOptions, type RequestCallOptions, type Result, type RetryOptions, type TokenProvider, type TransformResponseKeysMode, type UnknownPagination, type ValidationGroups, applyJsonApiHeaders, applyTransformKeys, assertValidIdempotencyKey, buildCursorPageParams, buildJsonApiQuery, buildOffsetPageParams, createApiClient, createHealthCheck, defaultIdempotencyKey, dispatchWithRetry, etagFromResponseHeaders, flattenAxiosHeaders, formatIfMatch, getHeader, getNextPageUrl, groupValidationErrorsByPointer, hasErrorCode, indexIncluded, isApiClientError, isApiClientErrorWithCode, isAuthenticationError, isConflictError, isForbiddenError, isIdempotencyKeyRequiredError, isIfMatchRequiredError, isMfaVerificationRequiredError, isMutationMethod, isPayloadTooLargeError, isPreconditionFailedError, isPreconditionRequiredError, isRetryablePerPolicy, isValidationError, normalizeAxiosResponse, normalizeHttpUrl, parseDeprecationHeaders, parseJsonApiDocument, parseJsonApiErrorBody, parseMultiStatusBody, parsePaginationKind, parseRetryAfterSeconds, pollAsyncResult, readResourceVersion, redactHeaderRecord, resolveAcceptLanguage, resolveAcceptedLocation, resolveAuthorizationHeader, resolveIncluded, resolveResourcePath, retryAllowed, truncateForLog };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// package.json
|
|
2
2
|
var package_default = {
|
|
3
3
|
name: "@vahidkaargar/customized-api-client",
|
|
4
|
-
version: "0.
|
|
4
|
+
version: "0.3.0",
|
|
5
5
|
description: "TypeScript Axios client for JSON:API v1.1 with idempotency, retries, and normalized results",
|
|
6
6
|
type: "module",
|
|
7
7
|
engines: {
|
|
@@ -87,14 +87,29 @@ function applyJsonApiHeaders(config, method) {
|
|
|
87
87
|
const m = method.toUpperCase();
|
|
88
88
|
const headers = { ...config.headers };
|
|
89
89
|
headers.Accept = headers.Accept ?? JSON_API;
|
|
90
|
-
if (
|
|
90
|
+
if (shouldSetJsonApiContentType(m, config)) {
|
|
91
91
|
headers["Content-Type"] = headers["Content-Type"] ?? JSON_API;
|
|
92
92
|
}
|
|
93
93
|
return { ...config, headers };
|
|
94
94
|
}
|
|
95
|
-
function
|
|
95
|
+
function shouldSetJsonApiContentType(method, config) {
|
|
96
96
|
if (!["POST", "PATCH", "PUT"].includes(method)) return false;
|
|
97
|
-
|
|
97
|
+
const data = config.data;
|
|
98
|
+
if (data === void 0 || data === null) return false;
|
|
99
|
+
return isJsonApiSerializableBody(data);
|
|
100
|
+
}
|
|
101
|
+
function isJsonApiSerializableBody(data) {
|
|
102
|
+
if (typeof data !== "object") return false;
|
|
103
|
+
if (Array.isArray(data)) return true;
|
|
104
|
+
if (typeof FormData !== "undefined" && data instanceof FormData) return false;
|
|
105
|
+
if (typeof Blob !== "undefined" && data instanceof Blob) return false;
|
|
106
|
+
if (data instanceof ArrayBuffer) return false;
|
|
107
|
+
if (ArrayBuffer.isView(data)) return false;
|
|
108
|
+
if (typeof URLSearchParams !== "undefined" && data instanceof URLSearchParams) return false;
|
|
109
|
+
if (data instanceof Date) return false;
|
|
110
|
+
if (typeof ReadableStream !== "undefined" && data instanceof ReadableStream) return false;
|
|
111
|
+
const proto = Object.getPrototypeOf(data);
|
|
112
|
+
return proto === Object.prototype || proto === null;
|
|
98
113
|
}
|
|
99
114
|
|
|
100
115
|
// src/headers/auth.ts
|
|
@@ -762,6 +777,9 @@ function createApiClient(config) {
|
|
|
762
777
|
async post(path, data, opts) {
|
|
763
778
|
return perform("POST", resolvePath(path), { ...opts, data });
|
|
764
779
|
},
|
|
780
|
+
async postFormData(path, data, opts) {
|
|
781
|
+
return perform("POST", resolvePath(path), { ...opts, data });
|
|
782
|
+
},
|
|
765
783
|
async patch(path, data, opts) {
|
|
766
784
|
return perform("PATCH", resolvePath(path), { ...opts, data });
|
|
767
785
|
},
|
|
@@ -794,20 +812,42 @@ function createApiClient(config) {
|
|
|
794
812
|
ifMatchVersion: version
|
|
795
813
|
});
|
|
796
814
|
},
|
|
797
|
-
safeGet: (path, opts) => safe(() =>
|
|
798
|
-
safePost: (path, data, opts) => safe(() =>
|
|
799
|
-
safePatch: (path, data, opts) => safe(() =>
|
|
800
|
-
safePut: (path, data, opts) => safe(() =>
|
|
801
|
-
safeDelete: (path, opts) => safe(() =>
|
|
802
|
-
safeHead: (path, opts) => safe(() =>
|
|
803
|
-
safeRequest: (ax, opts) => safe(() =>
|
|
804
|
-
|
|
805
|
-
|
|
815
|
+
safeGet: (path, opts) => safe(() => perform("GET", resolvePath(path), opts ?? {})),
|
|
816
|
+
safePost: (path, data, opts) => safe(() => perform("POST", resolvePath(path), { ...opts, data })),
|
|
817
|
+
safePatch: (path, data, opts) => safe(() => perform("PATCH", resolvePath(path), { ...opts, data })),
|
|
818
|
+
safePut: (path, data, opts) => safe(() => perform("PUT", resolvePath(path), { ...opts, data })),
|
|
819
|
+
safeDelete: (path, opts) => safe(() => perform("DELETE", resolvePath(path), opts ?? {})),
|
|
820
|
+
safeHead: (path, opts) => safe(() => perform("HEAD", resolvePath(path), opts ?? {})),
|
|
821
|
+
safeRequest: (ax, opts) => safe(() => {
|
|
822
|
+
const method = (ax.method ?? "GET").toUpperCase();
|
|
823
|
+
const rawUrl = ax.url ?? "/";
|
|
824
|
+
const u = typeof rawUrl === "string" && /^https?:\/\//i.test(rawUrl) ? rawUrl : resolvePath(rawUrl);
|
|
825
|
+
return perform(method, u, {
|
|
826
|
+
...opts,
|
|
827
|
+
data: ax.data,
|
|
828
|
+
idempotencyKey: opts?.idempotencyKey ?? readHeader(ax, "Idempotency-Key")
|
|
829
|
+
});
|
|
830
|
+
}),
|
|
831
|
+
safeGetByUrl: (fullUrl, opts) => safe(() => perform("GET", fullUrl, opts ?? {})),
|
|
832
|
+
safePatchWithVersion: (path, data, version, opts) => safe(
|
|
833
|
+
() => perform("PATCH", resolvePath(path), {
|
|
834
|
+
...opts,
|
|
835
|
+
data,
|
|
836
|
+
ifMatchVersion: version
|
|
837
|
+
})
|
|
838
|
+
)
|
|
806
839
|
};
|
|
807
840
|
return client;
|
|
808
841
|
}
|
|
809
842
|
|
|
810
843
|
// src/guards.ts
|
|
844
|
+
function hasErrorCode(error, code) {
|
|
845
|
+
if (!(error instanceof ApiClientError)) return false;
|
|
846
|
+
return error.errors.some((e) => e.code === code);
|
|
847
|
+
}
|
|
848
|
+
function isApiClientErrorWithCode(error, code) {
|
|
849
|
+
return hasErrorCode(error, code);
|
|
850
|
+
}
|
|
811
851
|
function isAuthenticationError(e) {
|
|
812
852
|
return isApiErr(e, 401);
|
|
813
853
|
}
|
|
@@ -817,6 +857,15 @@ function isForbiddenError(e) {
|
|
|
817
857
|
function isPreconditionRequiredError(e) {
|
|
818
858
|
return isApiErr(e, 428);
|
|
819
859
|
}
|
|
860
|
+
function isIdempotencyKeyRequiredError(e) {
|
|
861
|
+
return isApiErrWithCode(e, 428, "IDEMPOTENCY_KEY_REQUIRED");
|
|
862
|
+
}
|
|
863
|
+
function isIfMatchRequiredError(e) {
|
|
864
|
+
return isApiErrWithCode(e, 428, "IF_MATCH_REQUIRED");
|
|
865
|
+
}
|
|
866
|
+
function isMfaVerificationRequiredError(e) {
|
|
867
|
+
return isApiErrWithCode(e, 428, "MFA_VERIFICATION_REQUIRED");
|
|
868
|
+
}
|
|
820
869
|
function isPreconditionFailedError(e) {
|
|
821
870
|
return isApiErr(e, 412);
|
|
822
871
|
}
|
|
@@ -842,6 +891,9 @@ function isRetryablePerPolicy(e, policy) {
|
|
|
842
891
|
function isApiErr(e, status) {
|
|
843
892
|
return e instanceof ApiClientError && e.status === status;
|
|
844
893
|
}
|
|
894
|
+
function isApiErrWithCode(e, status, code) {
|
|
895
|
+
return isApiErr(e, status) && hasErrorCode(e, code);
|
|
896
|
+
}
|
|
845
897
|
|
|
846
898
|
// src/parse/pagination.ts
|
|
847
899
|
function parsePaginationKind(meta, links) {
|
|
@@ -1053,11 +1105,16 @@ export {
|
|
|
1053
1105
|
getHeader,
|
|
1054
1106
|
getNextPageUrl,
|
|
1055
1107
|
groupValidationErrorsByPointer,
|
|
1108
|
+
hasErrorCode,
|
|
1056
1109
|
indexIncluded,
|
|
1057
1110
|
isApiClientError,
|
|
1111
|
+
isApiClientErrorWithCode,
|
|
1058
1112
|
isAuthenticationError,
|
|
1059
1113
|
isConflictError,
|
|
1060
1114
|
isForbiddenError,
|
|
1115
|
+
isIdempotencyKeyRequiredError,
|
|
1116
|
+
isIfMatchRequiredError,
|
|
1117
|
+
isMfaVerificationRequiredError,
|
|
1061
1118
|
isMutationMethod,
|
|
1062
1119
|
isPayloadTooLargeError,
|
|
1063
1120
|
isPreconditionFailedError,
|