abaca-runtime
Advanced tools
+4
-4
| import { AsyncOrSync, Lookup } from './common.js'; | ||
| import { AllBodyMimeTypes, AllResponseMimeTypes, AllResponsesMatchingMimeType, BodiesMatchingMimeType, WithMimeTypeGlobs } from './mime-types.js'; | ||
| import { MimeType, OperationTypes } from './operations.js'; | ||
| import { ContentFormat, MimeType, OperationTypes } from './operations.js'; | ||
| export interface SdkConfigFor<O extends OperationTypes<keyof O & string>, F extends BaseFetch = typeof fetch> { | ||
@@ -55,3 +55,3 @@ /** API server address. */ | ||
| readonly operationId: string; | ||
| readonly contentType: string; | ||
| readonly content: ContentFormat; | ||
| readonly headers: RequestHeaders; | ||
@@ -67,3 +67,3 @@ readonly options?: RequestOptions<F>; | ||
| readonly operationId: string; | ||
| readonly contentType: string; | ||
| readonly content: ContentFormat; | ||
| readonly headers: RequestHeaders; | ||
@@ -78,4 +78,4 @@ readonly options?: RequestOptions<F>; | ||
| readonly accepted: ReadonlySet<MimeType>; | ||
| readonly declared: ReadonlySet<MimeType> | undefined; | ||
| readonly declared: ReadonlyMap<MimeType, ContentFormat> | undefined; | ||
| } | ||
| export {}; |
| import { Get, Has, KeysOfValues, Lookup, Values } from './common.js'; | ||
| import { MimeType, OperationDefinition, OperationType, OperationTypes, ResponseCode, ResponsesType } from './operations.js'; | ||
| import { ContentFormat, MimeType, OperationDefinition, OperationType, OperationTypes, ResponseCode, ResponsesType } from './operations.js'; | ||
| export type WithMimeTypeGlobs<M extends MimeType> = M | MimeTypePrefixes<M> | '*/*'; | ||
@@ -19,3 +19,3 @@ export declare const FALLBACK_MIME_TYPE = "*/*"; | ||
| }>; | ||
| export declare function contentTypeMatches(exact: MimeType, accepted: Iterable<MimeType>): boolean; | ||
| export declare function matchingContentType(exact: MimeType, accepted: Iterable<MimeType>): MimeType | undefined; | ||
| export declare class ByMimeType<V> { | ||
@@ -57,5 +57,9 @@ private readonly entries; | ||
| } | ||
| /** | ||
| * If returns true and `value` is not-null, it is guaranteed that `value` is a | ||
| * key in `declared`. | ||
| */ | ||
| export declare function isResponseTypeValid(args: { | ||
| readonly value: MimeType | undefined; | ||
| readonly declared: ReadonlySet<MimeType> | undefined; | ||
| readonly declared: ReadonlyMap<MimeType, ContentFormat> | undefined; | ||
| readonly accepted: ReadonlySet<MimeType>; | ||
@@ -66,4 +70,4 @@ }): boolean; | ||
| readonly code: ResponseCode; | ||
| readonly declared?: ReadonlySet<MimeType>; | ||
| readonly declared?: ReadonlyMap<MimeType, ContentFormat>; | ||
| } | ||
| export {}; |
+21
-13
@@ -10,14 +10,14 @@ export const FALLBACK_MIME_TYPE = '*/*'; | ||
| export const TEXT_MIME_TYPE = 'text/*'; | ||
| export function contentTypeMatches(exact, accepted) { | ||
| for (const item of accepted) { | ||
| if (exact === item || item === FALLBACK_MIME_TYPE) { | ||
| return true; | ||
| export function matchingContentType(exact, accepted) { | ||
| for (const elem of accepted) { | ||
| if (exact === elem || elem === FALLBACK_MIME_TYPE) { | ||
| return elem; | ||
| } | ||
| const got = exact.split('/'); | ||
| const want = item.split('/'); | ||
| const want = elem.split('/'); | ||
| if (got[0] === want[0] && (got[1] === want[1] || want[1] === '*')) { | ||
| return true; | ||
| return elem; | ||
| } | ||
| } | ||
| return false; | ||
| return undefined; | ||
| } | ||
@@ -57,5 +57,9 @@ export class ByMimeType { | ||
| const data = new Map(); | ||
| for (const [code, mtypes] of Object.entries(responses)) { | ||
| for (const [code, defs] of Object.entries(responses)) { | ||
| const ncode = +code; | ||
| data.set(isNaN(ncode) ? code : ncode, new Set(mtypes)); | ||
| const formats = new Map(); | ||
| for (const def of defs) { | ||
| formats.set(def.mimeType, def); | ||
| } | ||
| data.set(isNaN(ncode) ? code : ncode, formats); | ||
| } | ||
@@ -85,4 +89,4 @@ return new ResponseClauseMatcher(data); | ||
| let overlap = false; | ||
| for (const mtype of mtypes) { | ||
| if (contentTypeMatches(mtype, accepted)) { | ||
| for (const mtype of mtypes.keys()) { | ||
| if (matchingContentType(mtype, accepted) !== undefined) { | ||
| overlap = true; | ||
@@ -99,6 +103,10 @@ break; | ||
| } | ||
| /** | ||
| * If returns true and `value` is not-null, it is guaranteed that `value` is a | ||
| * key in `declared`. | ||
| */ | ||
| export function isResponseTypeValid(args) { | ||
| const { value, declared, accepted } = args; | ||
| if (declared == null) { | ||
| return value == null || contentTypeMatches(value, accepted); | ||
| return value == null || matchingContentType(value, accepted) != null; | ||
| } | ||
@@ -111,3 +119,3 @@ if (!declared.size) { | ||
| } | ||
| return contentTypeMatches(value, accepted); | ||
| return matchingContentType(value, accepted) != null; | ||
| } | ||
@@ -114,0 +122,0 @@ export function acceptedMimeTypes(header) { |
@@ -40,5 +40,10 @@ export type OperationTypes<N extends string = string> = { | ||
| readonly responses: { | ||
| readonly [code: ResponseCode]: ReadonlyArray<MimeType>; | ||
| readonly [code: ResponseCode]: ReadonlyArray<ContentFormat>; | ||
| }; | ||
| } | ||
| export interface ContentFormat { | ||
| readonly mimeType: MimeType; | ||
| readonly isBinary?: boolean; | ||
| readonly isStream?: boolean; | ||
| } | ||
| export interface ParameterDefinition { | ||
@@ -45,0 +50,0 @@ readonly location: ParameterLocation; |
+1
-1
| { | ||
| "name": "abaca-runtime", | ||
| "version": "0.11.1", | ||
| "version": "0.12.0", | ||
| "repository": "github:opvious/abaca", | ||
@@ -5,0 +5,0 @@ "description": "Abaca runtime utilities", |
25979
2.47%362
4.93%