@twin.org/web 0.0.3-next.31 → 0.0.3-next.33
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/es/models/IFetchOptions.js.map +1 -1
- package/dist/es/utils/fetchHelper.js +1 -1
- package/dist/es/utils/fetchHelper.js.map +1 -1
- package/dist/types/models/IFetchOptions.d.ts +1 -1
- package/docs/changelog.md +262 -222
- package/docs/reference/interfaces/IFetchOptions.md +0 -2
- package/package.json +4 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IFetchOptions.js","sourceRoot":"","sources":["../../../src/models/IFetchOptions.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IHttpHeaders } from \"./IHttpHeaders.js\";\n\n/**\n * Options for call to the fetch helper.\n */\nexport interface IFetchOptions {\n\t/**\n\t *
|
|
1
|
+
{"version":3,"file":"IFetchOptions.js","sourceRoot":"","sources":["../../../src/models/IFetchOptions.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IHttpHeaders } from \"./IHttpHeaders.js\";\n\n/**\n * Options for call to the fetch helper.\n */\nexport interface IFetchOptions {\n\t/**\n\t * The headers for the request.\n\t */\n\theaders?: IHttpHeaders;\n\n\t/**\n\t * Timeout for requests in milliseconds.\n\t */\n\ttimeoutMs?: number;\n\n\t/**\n\t * Include credentials in the requests.\n\t */\n\tincludeCredentials?: boolean;\n\n\t/**\n\t * The number of times to retry fetching defaults to no retries.\n\t */\n\tretryCount?: number;\n\n\t/**\n\t * The number of milliseconds we should delay before any retry.\n\t */\n\tretryDelayMs?: number;\n\n\t/**\n\t * The number of milliseconds to cache the response for, leave undefined for no cache, 0 means infinite.\n\t */\n\tcacheTtlMs?: number;\n}\n"]}
|
|
@@ -73,7 +73,7 @@ export class FetchHelper {
|
|
|
73
73
|
}, options?.timeoutMs);
|
|
74
74
|
}
|
|
75
75
|
let finalBody;
|
|
76
|
-
if (method === HttpMethod.POST || method === HttpMethod.PUT) {
|
|
76
|
+
if (method === HttpMethod.POST || method === HttpMethod.PUT || method === HttpMethod.PATCH) {
|
|
77
77
|
if (Is.string(body)) {
|
|
78
78
|
finalBody = body;
|
|
79
79
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchHelper.js","sourceRoot":"","sources":["../../../src/utils/fetchHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,YAAY,EAAe,MAAM,gBAAgB,CAAC;AAE9F,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAG7D,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD;;GAEG;AACH,MAAM,OAAO,WAAW;IACvB;;OAEG;IACI,MAAM,CAAU,UAAU,iBAAiC;IAElE;;;OAGG;IACK,MAAM,CAAU,aAAa,GAAW,QAAQ,CAAC;IAEzD;;;;;;;;OAQG;IACI,MAAM,CAAC,KAAK,CAAC,KAAK,CACxB,MAAc,EACd,GAAW,EACX,MAAkB,EAClB,IAA0B,EAC1B,OAAgD;QAEhD,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,UAAU,YAAkB,MAAM,CAAC,CAAC;QAC9D,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,UAAU,SAAe,GAAG,CAAC,CAAC;QACxD,MAAM,CAAC,UAAU,CAChB,WAAW,CAAC,UAAU,YAEtB,MAAM,EACN,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CACzB,C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SPDX-License-Identifier: Apache-2.0.\nimport { AsyncCache, BaseError, Guards, Is, ObjectHelper, type IError } from \"@twin.org/core\";\nimport { nameof, nameofCamelCase } from \"@twin.org/nameof\";\nimport { FetchError } from \"../errors/fetchError.js\";\nimport { HeaderTypes } from \"../models/headerTypes.js\";\nimport { HttpMethod } from \"../models/httpMethod.js\";\nimport { HttpStatusCode } from \"../models/httpStatusCode.js\";\nimport type { IFetchOptions } from \"../models/IFetchOptions.js\";\nimport type { IHttpHeaders } from \"../models/IHttpHeaders.js\";\nimport { MimeTypes } from \"../models/mimeTypes.js\";\n\n/**\n * Class to helper with fetch operations.\n */\nexport class FetchHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<FetchHelper>();\n\n\t/**\n\t * Prefix to use for cache entries.\n\t * @internal\n\t */\n\tprivate static readonly _CACHE_PREFIX: string = \"fetch_\";\n\n\t/**\n\t * Perform a fetch request.\n\t * @param source The source for the request.\n\t * @param url The url for the request.\n\t * @param method The http method.\n\t * @param body Request to send to the endpoint.\n\t * @param options Options for sending the requests.\n\t * @returns The response.\n\t */\n\tpublic static async fetch(\n\t\tsource: string,\n\t\turl: string,\n\t\tmethod: HttpMethod,\n\t\tbody?: string | Uint8Array,\n\t\toptions?: Omit<IFetchOptions, \"cacheTtlSeconds\">\n\t): Promise<Response> {\n\t\tGuards.string(FetchHelper.CLASS_NAME, nameof(source), source);\n\t\tGuards.string(FetchHelper.CLASS_NAME, nameof(url), url);\n\t\tGuards.arrayOneOf<HttpMethod>(\n\t\t\tFetchHelper.CLASS_NAME,\n\t\t\tnameof(method),\n\t\t\tmethod,\n\t\t\tObject.values(HttpMethod)\n\t\t);\n\t\tif (!Is.undefined(body) && !Is.uint8Array(body)) {\n\t\t\tGuards.string(FetchHelper.CLASS_NAME, nameof(body), body);\n\t\t}\n\t\tif (!Is.undefined(options)) {\n\t\t\tGuards.object<IFetchOptions>(FetchHelper.CLASS_NAME, nameof(options), options);\n\t\t\tif (!Is.undefined(options.headers)) {\n\t\t\t\tGuards.object<IHttpHeaders>(\n\t\t\t\t\tFetchHelper.CLASS_NAME,\n\t\t\t\t\tnameof(options.headers),\n\t\t\t\t\toptions.headers\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.timeoutMs)) {\n\t\t\t\tGuards.integer(FetchHelper.CLASS_NAME, nameof(options.timeoutMs), options.timeoutMs);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.includeCredentials)) {\n\t\t\t\tGuards.boolean(\n\t\t\t\t\tFetchHelper.CLASS_NAME,\n\t\t\t\t\tnameof(options.includeCredentials),\n\t\t\t\t\toptions.includeCredentials\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.retryCount)) {\n\t\t\t\tGuards.integer(FetchHelper.CLASS_NAME, nameof(options.retryCount), options.retryCount);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.retryDelayMs)) {\n\t\t\t\tGuards.integer(FetchHelper.CLASS_NAME, nameof(options.retryDelayMs), options.retryDelayMs);\n\t\t\t}\n\t\t}\n\n\t\tlet controller: AbortController | undefined;\n\t\tlet timerId: number | NodeJS.Timeout | undefined;\n\t\tconst retryCount = options?.retryCount ?? 1;\n\t\tconst baseDelayMilliseconds = options?.retryDelayMs ?? 3000;\n\n\t\tlet lastError: IError | undefined;\n\t\tlet attempt;\n\t\tfor (attempt = 0; attempt < retryCount; attempt++) {\n\t\t\tif (attempt > 0) {\n\t\t\t\tconst exponentialBackoffDelay = baseDelayMilliseconds * Math.pow(2, attempt - 1);\n\t\t\t\tawait new Promise(resolve => globalThis.setTimeout(resolve, exponentialBackoffDelay));\n\t\t\t}\n\n\t\t\tif (options?.timeoutMs !== undefined) {\n\t\t\t\tcontroller = new AbortController();\n\t\t\t\ttimerId = globalThis.setTimeout(() => {\n\t\t\t\t\tif (controller) {\n\t\t\t\t\t\tcontroller.abort();\n\t\t\t\t\t}\n\t\t\t\t}, options?.timeoutMs);\n\t\t\t}\n\n\t\t\tlet finalBody;\n\t\t\tif (method === HttpMethod.POST || method === HttpMethod.PUT) {\n\t\t\t\tif (Is.string(body)) {\n\t\t\t\t\tfinalBody = body;\n\t\t\t\t} else if (Is.uint8Array(body)) {\n\t\t\t\t\tfinalBody = new Uint8Array(body);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst requestOptions: RequestInit = {\n\t\t\t\t\tmethod,\n\t\t\t\t\theaders: options?.headers as HeadersInit,\n\t\t\t\t\tbody: finalBody,\n\t\t\t\t\tsignal: controller ? controller.signal : undefined\n\t\t\t\t};\n\t\t\t\tif (Is.boolean(options?.includeCredentials)) {\n\t\t\t\t\trequestOptions.credentials = \"include\";\n\t\t\t\t}\n\n\t\t\t\tconst response = await fetch(url, requestOptions);\n\n\t\t\t\tif (!response.ok && retryCount > 1) {\n\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.general`,\n\t\t\t\t\t\t(response.status as HttpStatusCode) ?? HttpStatusCode.internalServerError,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\turl,\n\t\t\t\t\t\t\tstatusText: response.statusText\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\treturn response;\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconst isErr = Is.object<IError>(err);\n\t\t\t\tif (isErr && Is.stringValue(err.message) && err.message.includes(\"Failed to fetch\")) {\n\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.connectivity`,\n\t\t\t\t\t\tHttpStatusCode.serviceUnavailable,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\turl\n\t\t\t\t\t\t},\n\t\t\t\t\t\terr\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tconst isAbort = isErr && err.name === \"AbortError\";\n\t\t\t\t\tconst props: { [id: string]: unknown } = { url };\n\t\t\t\t\tlet httpStatus: HttpStatusCode = HttpStatusCode.internalServerError;\n\t\t\t\t\tif (isAbort) {\n\t\t\t\t\t\thttpStatus = HttpStatusCode.requestTimeout;\n\t\t\t\t\t} else if (isErr && \"httpStatus\" in err) {\n\t\t\t\t\t\thttpStatus = err.httpStatus as HttpStatusCode;\n\t\t\t\t\t}\n\t\t\t\t\tif (isErr && \"statusText\" in err) {\n\t\t\t\t\t\tprops.statusText = err.statusText;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (isAbort) {\n\t\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\t\tsource,\n\t\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.timeout`,\n\t\t\t\t\t\t\thttpStatus,\n\t\t\t\t\t\t\tprops,\n\t\t\t\t\t\t\terr\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\t\tsource,\n\t\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.general`,\n\t\t\t\t\t\t\thttpStatus,\n\t\t\t\t\t\t\tprops,\n\t\t\t\t\t\t\terr\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\tif (timerId) {\n\t\t\t\t\tglobalThis.clearTimeout(timerId);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (retryCount > 1 && attempt === retryCount) {\n\t\t\tthrow new FetchError(\n\t\t\t\tsource,\n\t\t\t\t`${nameofCamelCase<FetchHelper>()}.retryLimitExceeded`,\n\t\t\t\tHttpStatusCode.internalServerError,\n\t\t\t\t{ url },\n\t\t\t\tlastError\n\t\t\t);\n\t\t}\n\n\t\tthrow lastError as Error;\n\t}\n\n\t/**\n\t * Perform a request in json format.\n\t * @param source The source for the request.\n\t * @param url The url for the request.\n\t * @param method The http method.\n\t * @param requestData Request to send to the endpoint.\n\t * @param options Options for sending the requests.\n\t * @returns The response.\n\t */\n\tpublic static async fetchJson<T, U>(\n\t\tsource: string,\n\t\turl: string,\n\t\tmethod: HttpMethod,\n\t\trequestData?: T,\n\t\toptions?: IFetchOptions\n\t): Promise<U> {\n\t\tif (Is.integer(options?.cacheTtlMs) && options.cacheTtlMs >= 0) {\n\t\t\t// The cache option is set, so call the same method again but without\n\t\t\t// the cache option to get the result and cache it.\n\t\t\tconst cacheResponse = AsyncCache.exec(\n\t\t\t\t`${FetchHelper._CACHE_PREFIX}${url}`,\n\t\t\t\toptions.cacheTtlMs,\n\t\t\t\tasync () =>\n\t\t\t\t\tFetchHelper.fetchJson<T, U>(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\turl,\n\t\t\t\t\t\tmethod,\n\t\t\t\t\t\trequestData,\n\t\t\t\t\t\tObjectHelper.omit(options, [\"cacheTtlMs\"])\n\t\t\t\t\t)\n\t\t\t);\n\n\t\t\t// If the return value is a promise return it, otherwise continue\n\t\t\t// with the regular processing.\n\t\t\tif (Is.promise(cacheResponse)) {\n\t\t\t\treturn cacheResponse;\n\t\t\t}\n\t\t}\n\n\t\toptions ??= {};\n\t\toptions.headers ??= {};\n\n\t\tif (\n\t\t\tIs.undefined(options.headers[HeaderTypes.ContentType]) &&\n\t\t\t(method === HttpMethod.POST || method === HttpMethod.PUT || method === HttpMethod.PATCH)\n\t\t) {\n\t\t\toptions.headers[HeaderTypes.ContentType] = MimeTypes.Json;\n\t\t}\n\n\t\tconst response = await FetchHelper.fetch(\n\t\t\tsource,\n\t\t\turl,\n\t\t\tmethod,\n\t\t\trequestData ? JSON.stringify(requestData) : undefined,\n\t\t\toptions\n\t\t);\n\n\t\tif (response.ok) {\n\t\t\tif (response.status === HttpStatusCode.noContent) {\n\t\t\t\treturn {} as U;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\treturn (await response.json()) as U;\n\t\t\t} catch (err) {\n\t\t\t\tthrow new FetchError(\n\t\t\t\t\tsource,\n\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.decodingJSON`,\n\t\t\t\t\tHttpStatusCode.badRequest,\n\t\t\t\t\t{ url },\n\t\t\t\t\terr\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tconst errorResponseData = await response.json();\n\t\tconst errorResponse = BaseError.fromError(errorResponseData);\n\t\tconst isErrorEmpty = BaseError.isEmpty(errorResponse);\n\n\t\tthrow new FetchError(\n\t\t\tsource,\n\t\t\t`${nameofCamelCase<FetchHelper>()}.failureStatusText`,\n\t\t\tresponse.status as HttpStatusCode,\n\t\t\t{\n\t\t\t\tstatusText: response.statusText,\n\t\t\t\turl,\n\t\t\t\tdata: isErrorEmpty ? errorResponseData : undefined\n\t\t\t},\n\t\t\tisErrorEmpty ? undefined : errorResponse\n\t\t);\n\t}\n\n\t/**\n\t * Perform a request for binary data.\n\t * @param source The source for the request.\n\t * @param url The url for the request.\n\t * @param method The http method.\n\t * @param requestData Request to send to the endpoint.\n\t * @param options Options for sending the requests.\n\t * @returns The response.\n\t */\n\tpublic static async fetchBinary<T>(\n\t\tsource: string,\n\t\turl: string,\n\t\tmethod: Extract<HttpMethod, \"GET\" | \"POST\">,\n\t\trequestData?: Uint8Array,\n\t\toptions?: IFetchOptions\n\t): Promise<Uint8Array | T> {\n\t\tif (Is.integer(options?.cacheTtlMs) && options.cacheTtlMs >= 0) {\n\t\t\t// The cache option is set, so call the same method again but without\n\t\t\t// the cache option to get the result and cache it.\n\t\t\tconst cacheResponse = AsyncCache.exec(\n\t\t\t\t`${FetchHelper._CACHE_PREFIX}${url}`,\n\t\t\t\toptions.cacheTtlMs * 1000,\n\t\t\t\tasync () =>\n\t\t\t\t\tFetchHelper.fetchBinary<T>(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\turl,\n\t\t\t\t\t\tmethod,\n\t\t\t\t\t\trequestData,\n\t\t\t\t\t\tObjectHelper.omit(options, [\"cacheTtlMs\"])\n\t\t\t\t\t)\n\t\t\t);\n\n\t\t\t// If the return value is a promise return it, otherwise continue\n\t\t\t// with the regular processing.\n\t\t\tif (Is.promise(cacheResponse)) {\n\t\t\t\treturn cacheResponse;\n\t\t\t}\n\t\t}\n\n\t\toptions ??= {};\n\t\toptions.headers ??= {};\n\n\t\tif (Is.undefined(options.headers[HeaderTypes.ContentType])) {\n\t\t\toptions.headers[HeaderTypes.ContentType] = MimeTypes.OctetStream;\n\t\t}\n\n\t\tconst response = await FetchHelper.fetch(source, url, method, requestData, options);\n\n\t\tif (response.ok) {\n\t\t\tif (method === HttpMethod.GET) {\n\t\t\t\tif (response.status === HttpStatusCode.noContent) {\n\t\t\t\t\treturn new Uint8Array();\n\t\t\t\t}\n\t\t\t\treturn new Uint8Array(await response.arrayBuffer());\n\t\t\t}\n\t\t\ttry {\n\t\t\t\treturn (await response.json()) as T;\n\t\t\t} catch (err) {\n\t\t\t\tthrow new FetchError(\n\t\t\t\t\tsource,\n\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.decodingJSON`,\n\t\t\t\t\tHttpStatusCode.badRequest,\n\t\t\t\t\t{ url },\n\t\t\t\t\terr\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tconst errorResponseData = await response.json();\n\t\tconst errorResponse = BaseError.fromError(errorResponseData);\n\t\tconst isErrorEmpty = BaseError.isEmpty(errorResponse);\n\n\t\tthrow new FetchError(\n\t\t\tsource,\n\t\t\t`${nameofCamelCase<FetchHelper>()}.failureStatusText`,\n\t\t\tresponse.status as HttpStatusCode,\n\t\t\t{\n\t\t\t\tstatusText: response.statusText,\n\t\t\t\turl,\n\t\t\t\tdata: isErrorEmpty ? errorResponseData : undefined\n\t\t\t},\n\t\t\tisErrorEmpty ? undefined : errorResponse\n\t\t);\n\t}\n\n\t/**\n\t * Clears the cache.\n\t */\n\tpublic static clearCache(): void {\n\t\tAsyncCache.clearCache(FetchHelper._CACHE_PREFIX);\n\t}\n\n\t/**\n\t * Get a cache entry.\n\t * @param url The url for the request.\n\t * @returns The cache entry if it exists.\n\t */\n\tpublic static async getCacheEntry<T>(url: string): Promise<T | undefined> {\n\t\treturn AsyncCache.get<T>(`${FetchHelper._CACHE_PREFIX}${url}`);\n\t}\n\n\t/**\n\t * Set a cache entry.\n\t * @param url The url for the request.\n\t * @param value The value to cache.\n\t * @returns The cache entry if it exists.\n\t */\n\tpublic static async setCacheEntry<T>(url: string, value: T): Promise<void> {\n\t\tawait AsyncCache.set<T>(`${FetchHelper._CACHE_PREFIX}${url}`, value);\n\t}\n\n\t/**\n\t * Remove a cache entry.\n\t * @param url The url for the request.\n\t */\n\tpublic static removeCacheEntry(url: string): void {\n\t\tAsyncCache.remove(`${FetchHelper._CACHE_PREFIX}${url}`);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"fetchHelper.js","sourceRoot":"","sources":["../../../src/utils/fetchHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,YAAY,EAAe,MAAM,gBAAgB,CAAC;AAE9F,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAG7D,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD;;GAEG;AACH,MAAM,OAAO,WAAW;IACvB;;OAEG;IACI,MAAM,CAAU,UAAU,iBAAiC;IAElE;;;OAGG;IACK,MAAM,CAAU,aAAa,GAAW,QAAQ,CAAC;IAEzD;;;;;;;;OAQG;IACI,MAAM,CAAC,KAAK,CAAC,KAAK,CACxB,MAAc,EACd,GAAW,EACX,MAAkB,EAClB,IAA0B,EAC1B,OAAgD;QAEhD,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,UAAU,YAAkB,MAAM,CAAC,CAAC;QAC9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,KAAK,CAAC,CAAC;IACtE,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,gBAAgB,CAAC,GAAW;QACzC,UAAU,CAAC,MAAM,CAAC,GAAG,WAAW,CAAC,aAAa,GAAG,GAAG,EAAE,CAAC,CAAC;IACzD,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { AsyncCache, BaseError, Guards, Is, ObjectHelper, type IError } from \"@twin.org/core\";\nimport { nameof, nameofCamelCase } from \"@twin.org/nameof\";\nimport { FetchError } from \"../errors/fetchError.js\";\nimport { HeaderTypes } from \"../models/headerTypes.js\";\nimport { HttpMethod } from \"../models/httpMethod.js\";\nimport { HttpStatusCode } from \"../models/httpStatusCode.js\";\nimport type { IFetchOptions } from \"../models/IFetchOptions.js\";\nimport type { IHttpHeaders } from \"../models/IHttpHeaders.js\";\nimport { MimeTypes } from \"../models/mimeTypes.js\";\n\n/**\n * Class to helper with fetch operations.\n */\nexport class FetchHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<FetchHelper>();\n\n\t/**\n\t * Prefix to use for cache entries.\n\t * @internal\n\t */\n\tprivate static readonly _CACHE_PREFIX: string = \"fetch_\";\n\n\t/**\n\t * Perform a fetch request.\n\t * @param source The source for the request.\n\t * @param url The url for the request.\n\t * @param method The http method.\n\t * @param body Request to send to the endpoint.\n\t * @param options Options for sending the requests.\n\t * @returns The response.\n\t */\n\tpublic static async fetch(\n\t\tsource: string,\n\t\turl: string,\n\t\tmethod: HttpMethod,\n\t\tbody?: string | Uint8Array,\n\t\toptions?: Omit<IFetchOptions, \"cacheTtlSeconds\">\n\t): Promise<Response> {\n\t\tGuards.string(FetchHelper.CLASS_NAME, nameof(source), source);\n\t\tGuards.string(FetchHelper.CLASS_NAME, nameof(url), url);\n\t\tGuards.arrayOneOf<HttpMethod>(\n\t\t\tFetchHelper.CLASS_NAME,\n\t\t\tnameof(method),\n\t\t\tmethod,\n\t\t\tObject.values(HttpMethod)\n\t\t);\n\t\tif (!Is.undefined(body) && !Is.uint8Array(body)) {\n\t\t\tGuards.string(FetchHelper.CLASS_NAME, nameof(body), body);\n\t\t}\n\t\tif (!Is.undefined(options)) {\n\t\t\tGuards.object<IFetchOptions>(FetchHelper.CLASS_NAME, nameof(options), options);\n\t\t\tif (!Is.undefined(options.headers)) {\n\t\t\t\tGuards.object<IHttpHeaders>(\n\t\t\t\t\tFetchHelper.CLASS_NAME,\n\t\t\t\t\tnameof(options.headers),\n\t\t\t\t\toptions.headers\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.timeoutMs)) {\n\t\t\t\tGuards.integer(FetchHelper.CLASS_NAME, nameof(options.timeoutMs), options.timeoutMs);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.includeCredentials)) {\n\t\t\t\tGuards.boolean(\n\t\t\t\t\tFetchHelper.CLASS_NAME,\n\t\t\t\t\tnameof(options.includeCredentials),\n\t\t\t\t\toptions.includeCredentials\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.retryCount)) {\n\t\t\t\tGuards.integer(FetchHelper.CLASS_NAME, nameof(options.retryCount), options.retryCount);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.retryDelayMs)) {\n\t\t\t\tGuards.integer(FetchHelper.CLASS_NAME, nameof(options.retryDelayMs), options.retryDelayMs);\n\t\t\t}\n\t\t}\n\n\t\tlet controller: AbortController | undefined;\n\t\tlet timerId: number | NodeJS.Timeout | undefined;\n\t\tconst retryCount = options?.retryCount ?? 1;\n\t\tconst baseDelayMilliseconds = options?.retryDelayMs ?? 3000;\n\n\t\tlet lastError: IError | undefined;\n\t\tlet attempt;\n\t\tfor (attempt = 0; attempt < retryCount; attempt++) {\n\t\t\tif (attempt > 0) {\n\t\t\t\tconst exponentialBackoffDelay = baseDelayMilliseconds * Math.pow(2, attempt - 1);\n\t\t\t\tawait new Promise(resolve => globalThis.setTimeout(resolve, exponentialBackoffDelay));\n\t\t\t}\n\n\t\t\tif (options?.timeoutMs !== undefined) {\n\t\t\t\tcontroller = new AbortController();\n\t\t\t\ttimerId = globalThis.setTimeout(() => {\n\t\t\t\t\tif (controller) {\n\t\t\t\t\t\tcontroller.abort();\n\t\t\t\t\t}\n\t\t\t\t}, options?.timeoutMs);\n\t\t\t}\n\n\t\t\tlet finalBody;\n\t\t\tif (method === HttpMethod.POST || method === HttpMethod.PUT || method === HttpMethod.PATCH) {\n\t\t\t\tif (Is.string(body)) {\n\t\t\t\t\tfinalBody = body;\n\t\t\t\t} else if (Is.uint8Array(body)) {\n\t\t\t\t\tfinalBody = new Uint8Array(body);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst requestOptions: RequestInit = {\n\t\t\t\t\tmethod,\n\t\t\t\t\theaders: options?.headers as HeadersInit,\n\t\t\t\t\tbody: finalBody,\n\t\t\t\t\tsignal: controller ? controller.signal : undefined\n\t\t\t\t};\n\t\t\t\tif (Is.boolean(options?.includeCredentials)) {\n\t\t\t\t\trequestOptions.credentials = \"include\";\n\t\t\t\t}\n\n\t\t\t\tconst response = await fetch(url, requestOptions);\n\n\t\t\t\tif (!response.ok && retryCount > 1) {\n\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.general`,\n\t\t\t\t\t\t(response.status as HttpStatusCode) ?? HttpStatusCode.internalServerError,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\turl,\n\t\t\t\t\t\t\tstatusText: response.statusText\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\treturn response;\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconst isErr = Is.object<IError>(err);\n\t\t\t\tif (isErr && Is.stringValue(err.message) && err.message.includes(\"Failed to fetch\")) {\n\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.connectivity`,\n\t\t\t\t\t\tHttpStatusCode.serviceUnavailable,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\turl\n\t\t\t\t\t\t},\n\t\t\t\t\t\terr\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tconst isAbort = isErr && err.name === \"AbortError\";\n\t\t\t\t\tconst props: { [id: string]: unknown } = { url };\n\t\t\t\t\tlet httpStatus: HttpStatusCode = HttpStatusCode.internalServerError;\n\t\t\t\t\tif (isAbort) {\n\t\t\t\t\t\thttpStatus = HttpStatusCode.requestTimeout;\n\t\t\t\t\t} else if (isErr && \"httpStatus\" in err) {\n\t\t\t\t\t\thttpStatus = err.httpStatus as HttpStatusCode;\n\t\t\t\t\t}\n\t\t\t\t\tif (isErr && \"statusText\" in err) {\n\t\t\t\t\t\tprops.statusText = err.statusText;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (isAbort) {\n\t\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\t\tsource,\n\t\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.timeout`,\n\t\t\t\t\t\t\thttpStatus,\n\t\t\t\t\t\t\tprops,\n\t\t\t\t\t\t\terr\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\t\tsource,\n\t\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.general`,\n\t\t\t\t\t\t\thttpStatus,\n\t\t\t\t\t\t\tprops,\n\t\t\t\t\t\t\terr\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\tif (timerId) {\n\t\t\t\t\tglobalThis.clearTimeout(timerId);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (retryCount > 1 && attempt === retryCount) {\n\t\t\tthrow new FetchError(\n\t\t\t\tsource,\n\t\t\t\t`${nameofCamelCase<FetchHelper>()}.retryLimitExceeded`,\n\t\t\t\tHttpStatusCode.internalServerError,\n\t\t\t\t{ url },\n\t\t\t\tlastError\n\t\t\t);\n\t\t}\n\n\t\tthrow lastError as Error;\n\t}\n\n\t/**\n\t * Perform a request in json format.\n\t * @param source The source for the request.\n\t * @param url The url for the request.\n\t * @param method The http method.\n\t * @param requestData Request to send to the endpoint.\n\t * @param options Options for sending the requests.\n\t * @returns The response.\n\t */\n\tpublic static async fetchJson<T, U>(\n\t\tsource: string,\n\t\turl: string,\n\t\tmethod: HttpMethod,\n\t\trequestData?: T,\n\t\toptions?: IFetchOptions\n\t): Promise<U> {\n\t\tif (Is.integer(options?.cacheTtlMs) && options.cacheTtlMs >= 0) {\n\t\t\t// The cache option is set, so call the same method again but without\n\t\t\t// the cache option to get the result and cache it.\n\t\t\tconst cacheResponse = AsyncCache.exec(\n\t\t\t\t`${FetchHelper._CACHE_PREFIX}${url}`,\n\t\t\t\toptions.cacheTtlMs,\n\t\t\t\tasync () =>\n\t\t\t\t\tFetchHelper.fetchJson<T, U>(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\turl,\n\t\t\t\t\t\tmethod,\n\t\t\t\t\t\trequestData,\n\t\t\t\t\t\tObjectHelper.omit(options, [\"cacheTtlMs\"])\n\t\t\t\t\t)\n\t\t\t);\n\n\t\t\t// If the return value is a promise return it, otherwise continue\n\t\t\t// with the regular processing.\n\t\t\tif (Is.promise(cacheResponse)) {\n\t\t\t\treturn cacheResponse;\n\t\t\t}\n\t\t}\n\n\t\toptions ??= {};\n\t\toptions.headers ??= {};\n\n\t\tif (\n\t\t\tIs.undefined(options.headers[HeaderTypes.ContentType]) &&\n\t\t\t(method === HttpMethod.POST || method === HttpMethod.PUT || method === HttpMethod.PATCH)\n\t\t) {\n\t\t\toptions.headers[HeaderTypes.ContentType] = MimeTypes.Json;\n\t\t}\n\n\t\tconst response = await FetchHelper.fetch(\n\t\t\tsource,\n\t\t\turl,\n\t\t\tmethod,\n\t\t\trequestData ? JSON.stringify(requestData) : undefined,\n\t\t\toptions\n\t\t);\n\n\t\tif (response.ok) {\n\t\t\tif (response.status === HttpStatusCode.noContent) {\n\t\t\t\treturn {} as U;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\treturn (await response.json()) as U;\n\t\t\t} catch (err) {\n\t\t\t\tthrow new FetchError(\n\t\t\t\t\tsource,\n\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.decodingJSON`,\n\t\t\t\t\tHttpStatusCode.badRequest,\n\t\t\t\t\t{ url },\n\t\t\t\t\terr\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tconst errorResponseData = await response.json();\n\t\tconst errorResponse = BaseError.fromError(errorResponseData);\n\t\tconst isErrorEmpty = BaseError.isEmpty(errorResponse);\n\n\t\tthrow new FetchError(\n\t\t\tsource,\n\t\t\t`${nameofCamelCase<FetchHelper>()}.failureStatusText`,\n\t\t\tresponse.status as HttpStatusCode,\n\t\t\t{\n\t\t\t\tstatusText: response.statusText,\n\t\t\t\turl,\n\t\t\t\tdata: isErrorEmpty ? errorResponseData : undefined\n\t\t\t},\n\t\t\tisErrorEmpty ? undefined : errorResponse\n\t\t);\n\t}\n\n\t/**\n\t * Perform a request for binary data.\n\t * @param source The source for the request.\n\t * @param url The url for the request.\n\t * @param method The http method.\n\t * @param requestData Request to send to the endpoint.\n\t * @param options Options for sending the requests.\n\t * @returns The response.\n\t */\n\tpublic static async fetchBinary<T>(\n\t\tsource: string,\n\t\turl: string,\n\t\tmethod: Extract<HttpMethod, \"GET\" | \"POST\">,\n\t\trequestData?: Uint8Array,\n\t\toptions?: IFetchOptions\n\t): Promise<Uint8Array | T> {\n\t\tif (Is.integer(options?.cacheTtlMs) && options.cacheTtlMs >= 0) {\n\t\t\t// The cache option is set, so call the same method again but without\n\t\t\t// the cache option to get the result and cache it.\n\t\t\tconst cacheResponse = AsyncCache.exec(\n\t\t\t\t`${FetchHelper._CACHE_PREFIX}${url}`,\n\t\t\t\toptions.cacheTtlMs * 1000,\n\t\t\t\tasync () =>\n\t\t\t\t\tFetchHelper.fetchBinary<T>(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\turl,\n\t\t\t\t\t\tmethod,\n\t\t\t\t\t\trequestData,\n\t\t\t\t\t\tObjectHelper.omit(options, [\"cacheTtlMs\"])\n\t\t\t\t\t)\n\t\t\t);\n\n\t\t\t// If the return value is a promise return it, otherwise continue\n\t\t\t// with the regular processing.\n\t\t\tif (Is.promise(cacheResponse)) {\n\t\t\t\treturn cacheResponse;\n\t\t\t}\n\t\t}\n\n\t\toptions ??= {};\n\t\toptions.headers ??= {};\n\n\t\tif (Is.undefined(options.headers[HeaderTypes.ContentType])) {\n\t\t\toptions.headers[HeaderTypes.ContentType] = MimeTypes.OctetStream;\n\t\t}\n\n\t\tconst response = await FetchHelper.fetch(source, url, method, requestData, options);\n\n\t\tif (response.ok) {\n\t\t\tif (method === HttpMethod.GET) {\n\t\t\t\tif (response.status === HttpStatusCode.noContent) {\n\t\t\t\t\treturn new Uint8Array();\n\t\t\t\t}\n\t\t\t\treturn new Uint8Array(await response.arrayBuffer());\n\t\t\t}\n\t\t\ttry {\n\t\t\t\treturn (await response.json()) as T;\n\t\t\t} catch (err) {\n\t\t\t\tthrow new FetchError(\n\t\t\t\t\tsource,\n\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.decodingJSON`,\n\t\t\t\t\tHttpStatusCode.badRequest,\n\t\t\t\t\t{ url },\n\t\t\t\t\terr\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tconst errorResponseData = await response.json();\n\t\tconst errorResponse = BaseError.fromError(errorResponseData);\n\t\tconst isErrorEmpty = BaseError.isEmpty(errorResponse);\n\n\t\tthrow new FetchError(\n\t\t\tsource,\n\t\t\t`${nameofCamelCase<FetchHelper>()}.failureStatusText`,\n\t\t\tresponse.status as HttpStatusCode,\n\t\t\t{\n\t\t\t\tstatusText: response.statusText,\n\t\t\t\turl,\n\t\t\t\tdata: isErrorEmpty ? errorResponseData : undefined\n\t\t\t},\n\t\t\tisErrorEmpty ? undefined : errorResponse\n\t\t);\n\t}\n\n\t/**\n\t * Clears the cache.\n\t */\n\tpublic static clearCache(): void {\n\t\tAsyncCache.clearCache(FetchHelper._CACHE_PREFIX);\n\t}\n\n\t/**\n\t * Get a cache entry.\n\t * @param url The url for the request.\n\t * @returns The cache entry if it exists.\n\t */\n\tpublic static async getCacheEntry<T>(url: string): Promise<T | undefined> {\n\t\treturn AsyncCache.get<T>(`${FetchHelper._CACHE_PREFIX}${url}`);\n\t}\n\n\t/**\n\t * Set a cache entry.\n\t * @param url The url for the request.\n\t * @param value The value to cache.\n\t * @returns The cache entry if it exists.\n\t */\n\tpublic static async setCacheEntry<T>(url: string, value: T): Promise<void> {\n\t\tawait AsyncCache.set<T>(`${FetchHelper._CACHE_PREFIX}${url}`, value);\n\t}\n\n\t/**\n\t * Remove a cache entry.\n\t * @param url The url for the request.\n\t */\n\tpublic static removeCacheEntry(url: string): void {\n\t\tAsyncCache.remove(`${FetchHelper._CACHE_PREFIX}${url}`);\n\t}\n}\n"]}
|