@animo-id/oauth2-utils
Advanced tools
Comparing version 0.0.2-alpha-20241107141610 to 0.0.2-alpha-20241107144122
@@ -30,2 +30,8 @@ import * as v from 'valibot'; | ||
declare class InvalidFetchResponseError extends Error { | ||
readonly textResponse: string; | ||
readonly response: FetchResponse; | ||
constructor(message: string, textResponse: string, response: FetchResponse); | ||
} | ||
/** | ||
@@ -71,3 +77,11 @@ * Get the time in seconds since epoch for a date. | ||
type ValibotFetcher = <Schema extends v.BaseSchema<any, any, any>>(schema: Schema, ...args: Parameters<Fetch>) => Promise<{ | ||
declare enum ContentType { | ||
XWwwFormUrlencoded = "application/x-www-form-urlencoded", | ||
Json = "application/json", | ||
JwkSet = "application/jwk-set+json" | ||
} | ||
declare function isContentType(contentType: ContentType, value: string): boolean; | ||
declare function isResponseContentType(contentType: ContentType, response: FetchResponse): boolean; | ||
type ValibotFetcher = <Schema extends v.BaseSchema<any, any, any>>(schema: Schema, expectedContentType: ContentType, ...args: Parameters<Fetch>) => Promise<{ | ||
response: Awaited<ReturnType<Fetch>>; | ||
@@ -134,9 +148,2 @@ result?: v.SafeParseResult<Schema>; | ||
declare enum ContentType { | ||
XWwwFormUrlencoded = "application/x-www-form-urlencoded", | ||
Json = "application/json" | ||
} | ||
declare function isContentType(contentType: ContentType, value: string): boolean; | ||
declare function isResponseContentType(contentType: ContentType, response: FetchResponse): boolean; | ||
interface Oid4vcTsConfig { | ||
@@ -153,2 +160,2 @@ /** | ||
export { type BaseSchema, ContentType, type Fetch, type FetchHeaders, type FetchRequestInit, type FetchResponse, _Headers as Headers, type HttpMethod, type InferOutputUnion, JsonParseError, type Oid4vcTsConfig, type Optional, type Simplify, _URL as URL, _URLSearchParams as URLSearchParams, type ValibotFetcher, ValidationError, type WwwAuthenticateHeaderChallenge, addSecondsToDate, arrayEqualsIgnoreOrder, createValibotFetcher, dateToSeconds, decodeBase64, decodeUtf8String, defaultFetcher, encodeToBase64, encodeToBase64Url, encodeToUtf8String, encodeWwwAuthenticateHeader, getGlobalConfig, getQueryParams, isContentType, isResponseContentType, joinUriParts, mergeDeep, objectToQueryParams, parseWithErrorHandling, parseWwwAuthenticateHeader, setGlobalConfig, stringToJsonWithErrorHandling, vHttpMethod, vHttpsUrl, vInteger, valibotRecursiveFlattenIssues }; | ||
export { type BaseSchema, ContentType, type Fetch, type FetchHeaders, type FetchRequestInit, type FetchResponse, _Headers as Headers, type HttpMethod, type InferOutputUnion, InvalidFetchResponseError, JsonParseError, type Oid4vcTsConfig, type Optional, type Simplify, _URL as URL, _URLSearchParams as URLSearchParams, type ValibotFetcher, ValidationError, type WwwAuthenticateHeaderChallenge, addSecondsToDate, arrayEqualsIgnoreOrder, createValibotFetcher, dateToSeconds, decodeBase64, decodeUtf8String, defaultFetcher, encodeToBase64, encodeToBase64Url, encodeToUtf8String, encodeWwwAuthenticateHeader, getGlobalConfig, getQueryParams, isContentType, isResponseContentType, joinUriParts, mergeDeep, objectToQueryParams, parseWithErrorHandling, parseWwwAuthenticateHeader, setGlobalConfig, stringToJsonWithErrorHandling, vHttpMethod, vHttpsUrl, vInteger, valibotRecursiveFlattenIssues }; |
@@ -35,2 +35,3 @@ "use strict"; | ||
Headers: () => _Headers, | ||
InvalidFetchResponseError: () => InvalidFetchResponseError, | ||
JsonParseError: () => JsonParseError, | ||
@@ -143,2 +144,12 @@ URL: () => _URL, | ||
// src/error/InvalidFetchResponseError.ts | ||
var InvalidFetchResponseError = class extends Error { | ||
constructor(message, textResponse, response) { | ||
super(`${message} | ||
${textResponse}`); | ||
this.textResponse = textResponse; | ||
this.response = response.clone(); | ||
} | ||
}; | ||
// src/date.ts | ||
@@ -219,2 +230,3 @@ function dateToSeconds(date) { | ||
ContentType2["Json"] = "application/json"; | ||
ContentType2["JwkSet"] = "application/jwk-set+json"; | ||
return ContentType2; | ||
@@ -234,7 +246,14 @@ })(ContentType || {}); | ||
function createValibotFetcher(fetcher = defaultFetcher) { | ||
return async (schema, ...args) => { | ||
return async (schema, expectedContentType, ...args) => { | ||
const response = await fetcher(...args); | ||
if (response.ok && !isResponseContentType(expectedContentType, response)) { | ||
throw new InvalidFetchResponseError( | ||
`Expected response to match content type '${expectedContentType}', but received '${response.headers.get("Content-Type")}'`, | ||
await response.clone().text(), | ||
response | ||
); | ||
} | ||
return { | ||
response, | ||
result: response.ok && isResponseContentType("application/json" /* Json */, response) ? v2.safeParse(schema, await response.json()) : void 0 | ||
result: response.ok ? v2.safeParse(schema, await response.json()) : void 0 | ||
}; | ||
@@ -344,2 +363,3 @@ }; | ||
Headers, | ||
InvalidFetchResponseError, | ||
JsonParseError, | ||
@@ -346,0 +366,0 @@ URL, |
{ | ||
"name": "@animo-id/oauth2-utils", | ||
"version": "0.0.2-alpha-20241107141610", | ||
"version": "0.0.2-alpha-20241107144122", | ||
"exports": { | ||
@@ -5,0 +5,0 @@ ".": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
81587
815