UNPKG

api-wizard

Version:

A powerful TypeScript wrapper for native Fetch API with token management, interceptors, and type-safe HTTP requests

40 lines (39 loc) 1.75 kB
import { ParsedQs } from "qs"; type SchemaField = { type: "number"; default?: number; } | { type: "string"; default?: string; }; type Schema = Record<string, SchemaField>; type ParsedParamsFromSchema<S extends Schema> = { [K in keyof S]: S[K] extends { type: "number"; } ? number : S[K] extends { type: "string"; } ? string : never; }; type Options<S extends Schema> = { defaults?: Partial<ParsedParamsFromSchema<S>>; numberDefault?: number; stringDefault?: string; /** 스키마 외 키 허용 여부 (기본 true) */ allowUnknown?: boolean; }; /** 입력: req.params / req.query / Next searchParams 전부 수용 */ type Input = ParsedQs | Record<string, string | string[] | undefined>; /** 평탄화: string | undefined 만 남기기 */ declare function normalizeQueryLike(input: Input): Record<string, string | undefined>; /** 추가 키 타입: 유니온 배열의 요소 타입에서 키를 뽑아 스키마 키를 제외 */ type UnionOfArray<T extends readonly unknown[]> = T[number]; type FlatFromInput<I> = I extends Record<string, infer V> ? Record<string, V> : never; type ExtraKeys<Inputs extends readonly unknown[], S extends Schema> = Exclude<keyof FlatFromInput<UnionOfArray<Inputs>>, keyof S>; /** 🔧 출력에서 undefined 제거: 스키마 외 키도 반드시 string */ type ParsedWithExtras<Inputs extends readonly unknown[], S extends Schema> = ParsedParamsFromSchema<S> & { [K in ExtraKeys<Inputs, S>]: string; }; export declare function createParsedParams<S extends Schema>(schema: S, opts?: Options<S>): (<Inputs extends readonly Input[]>(inputs: Inputs) => Promise<ParsedWithExtras<Inputs, S>>) & { normalize: typeof normalizeQueryLike; }; export {};