@hulla/api-request
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,115 +0,325 @@ | ||
type Args = readonly unknown[]; | ||
type Methods$1 = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'CONNECT' | 'TRACE'; | ||
type Fn<A extends Args, R> = (...args: A) => R; | ||
type Context<N extends string, CN extends string, A extends Args, RR extends string = string> = { | ||
method: CN; | ||
type: CN extends 'call' ? 'procedure' : CN extends LowercaseMethods$1 ? 'request' : 'custom'; | ||
route: N; | ||
routerName: RR; | ||
args: A; | ||
}; | ||
type ResolverArgs<CTX, R> = [R, CTX]; | ||
type Resolver<CTX, R, R2 = R> = Fn<ResolverArgs<CTX, R>, R2>; | ||
type Call<N extends string, CN extends string, CTX, A extends Args, R, R2 = R> = { | ||
route: N; | ||
fn: Fn<A, R>; | ||
resolver?: Resolver<CTX, R, R2>; | ||
method: CN; | ||
}; | ||
type LowercaseMethods$1 = { | ||
[K in Methods$1]: Lowercase<K>; | ||
}[Methods$1]; | ||
import * as _hulla_api from '@hulla/api' | ||
import { Args, Call, Context, Fn, Obj, Resolver, TypeError } from '@hulla/api' | ||
type Methods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'CONNECT' | 'TRACE'; | ||
type Methods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'CONNECT' | 'TRACE' | ||
type LowercaseMethods = { | ||
[K in Methods]: Lowercase<K>; | ||
}[Methods]; | ||
type StaticConfig<M extends Methods | LowercaseMethods, Data = unknown, Params = unknown> = Partial<RequestInit & { | ||
method?: M; | ||
data?: Data; | ||
params?: Params; | ||
}> & { | ||
url: URL | string; | ||
}; | ||
type TypedRequestConfig<M extends LowercaseMethods | Methods, Data = unknown, Params = unknown> = Uppercase<M> extends 'HEAD' | 'GET' ? StaticConfig<Lowercase<M> | Uppercase<M>, Data, Params> & { | ||
body?: `ERROR: method ${M} cannot contain a body`; | ||
bodyUsed?: false; | ||
} : StaticConfig<Lowercase<M> | Uppercase<M>, Data, Params>; | ||
[K in Methods]: Lowercase<K> | ||
}[Methods] | ||
type StaticConfig<M extends Methods | LowercaseMethods, U extends string = string, P extends boolean = true> = Partial< | ||
RequestInit & { | ||
method?: M | ||
data?: unknown | ||
checkParams?: P | ||
params?: Record<string, URICompoent> | ||
} | ||
> & { | ||
readonly url: U | ||
} & (Uppercase<M> extends 'HEAD' | 'GET' | ||
? { | ||
body?: TypeError< | ||
`ERROR: method "${M}" cannot contain a body`, | ||
'https://developer.mozilla.org/en-US/docs/Web/API/Request/body' | ||
> | ||
} | ||
: {}) | ||
type TypedRequestConfig< | ||
M extends LowercaseMethods | Methods, | ||
U extends string = string, | ||
P extends boolean = true, | ||
> = StaticConfig<M, U, P> & | ||
(P extends true | ||
? U extends string | ||
? HasPath<U> extends never | ||
? ParseSearch<U> extends never | ||
? {} | ||
: { | ||
params: URLToParams<U> | ||
} | ||
: { | ||
params: URLToParams<U> | ||
} | ||
: {} | ||
: {}) | ||
type RequestMap<CTX> = { | ||
[M in LowercaseMethods]: <const N extends string, Data, Params, const R extends TypedRequestConfig<M | Uppercase<M>, Data, Params> | string | URL, A extends Args = [], const R2 = Promise<Response>>(route: N, configOrConfigFn: Fn<A, R> | R, resolver?: Resolver<CTX & Context<N, M, A>, R, R2>) => Call<N, M, CTX, A, R, R2>; | ||
}; | ||
type URLType = InstanceType<typeof URL>; | ||
[M in LowercaseMethods]: < | ||
const N extends string, | ||
const R extends TypedRequestConfig<M | Uppercase<M>> | string | URL | Request, | ||
A extends Args = [], | ||
const R2 = Promise<Response>, | ||
>( | ||
route: N, | ||
configOrConfigFn: Fn<A, R> | R, | ||
resolver?: Resolver<CTX & Context<N, M, A>, R, R2> | ||
) => Call<N, M, CTX, A, R, R2> | ||
} | ||
type URLType = InstanceType<typeof URL> | ||
type ExtractUrl<U> = U extends | ||
| { | ||
url: string | ||
} | ||
| string | ||
? U extends { | ||
url: string | ||
} | ||
? U['url'] | ||
: U | ||
: never | ||
type URICompoent = string | number | boolean | ||
type ParsePath< | ||
U extends | ||
| string | ||
| { | ||
url: string | ||
}, | ||
> = | ||
ExtractUrl<U> extends `${string}/:${infer Param}/${infer Rest}` | ||
? { | ||
[K in Param]: URICompoent | ||
} & ParsePath<Rest> | ||
: ExtractUrl<U> extends `${string}/:${infer Param}` | ||
? { | ||
[K in Param]: URICompoent | ||
} | ||
: {} | ||
type HasPath< | ||
U extends | ||
| string | ||
| { | ||
url: string | ||
}, | ||
> = | ||
ExtractUrl<U> extends `${string}/:${infer Param}/${infer Rest}` | ||
? { | ||
[K in Param]: URICompoent | ||
} & ParsePath<Rest> | ||
: ExtractUrl<U> extends `${string}/:${infer Param}` | ||
? { | ||
[K in Param]: URICompoent | ||
} | ||
: never | ||
type ParseSearch< | ||
U extends | ||
| string | ||
| { | ||
url: string | ||
}, | ||
> = | ||
ExtractUrl<U> extends `${string}?${infer Params}` | ||
? Params extends `${infer Key}&${infer Rest}` | ||
? { | ||
[K in Key]: URICompoent | ||
} & ParseSearch<`?${Rest}`> | ||
: Params extends `${infer Key}` | ||
? { | ||
[K in Key]: URICompoent | ||
} | ||
: never | ||
: never | ||
type URLToParams< | ||
U extends | ||
| string | ||
| { | ||
url: string | ||
}, | ||
> = U extends | ||
| { | ||
url: string | ||
} | ||
| string | ||
? { | ||
[K in keyof ParsePath<U>]: ParsePath<U>[K] | ||
} & { | ||
[K in keyof ParseSearch<U>]: ParseSearch<U>[K] | ||
} | ||
: never | ||
type Callables<T> = { | ||
[K in keyof T]: T[K] extends (...args: any[]) => any ? K : never | ||
}[keyof T] | ||
declare function resolve<R, Req extends TypedRequestConfig<LowercaseMethods | Methods> | string | URL = TypedRequestConfig<LowercaseMethods | Methods> | string | URL>(req: Req, ctx: Context<string, LowercaseMethods, Args, string>): R; | ||
declare function resolve< | ||
R, | ||
Req extends TypedRequestConfig<LowercaseMethods | Methods> | string | URL = | ||
| TypedRequestConfig<LowercaseMethods | Methods> | ||
| string | ||
| URL, | ||
T extends Callables<Response> = 'json', | ||
>(req: Req, ctx: Context<string, LowercaseMethods, Args, string>, transformer?: T): R | ||
declare function parseUrl(req: TypedRequestConfig<LowercaseMethods, unknown, unknown> | URL | string | Request): URL; | ||
declare function parseBody(req: TypedRequestConfig<LowercaseMethods | Methods, unknown, unknown> | URL | string | Request): RequestInit['body']; | ||
declare function parseRequest<M extends LowercaseMethods>(req: TypedRequestConfig<LowercaseMethods | Methods, unknown, unknown> | URL | string | Request, url: URL, body: RequestInit['body'], ctx: Context<string, M, Args, string>): { | ||
body?: BodyInit | null | undefined; | ||
url: URL; | ||
method: M; | ||
} | { | ||
body?: BodyInit | null | undefined; | ||
url: URL; | ||
hash: string; | ||
host: string; | ||
hostname: string; | ||
href: string; | ||
toString(): string; | ||
origin: string; | ||
password: string; | ||
pathname: string; | ||
port: string; | ||
protocol: string; | ||
search: string; | ||
searchParams: URLSearchParams; | ||
username: string; | ||
toJSON(): string; | ||
createObjectURL(obj: Blob): string; | ||
revokeObjectURL(url: string): void; | ||
canParse(url: string, base?: string | undefined): boolean; | ||
method: M; | ||
} | { | ||
body: BodyInit | null; | ||
url: URL; | ||
cache: RequestCache; | ||
credentials: RequestCredentials; | ||
destination: RequestDestination; | ||
headers: Headers; | ||
integrity: string; | ||
keepalive: boolean; | ||
method: string; | ||
mode: RequestMode; | ||
redirect: RequestRedirect; | ||
referrer: string; | ||
referrerPolicy: ReferrerPolicy; | ||
signal: AbortSignal; | ||
clone(): Request; | ||
bodyUsed: boolean; | ||
arrayBuffer(): Promise<ArrayBuffer>; | ||
blob(): Promise<Blob>; | ||
formData(): Promise<FormData>; | ||
json(): Promise<any>; | ||
text(): Promise<string>; | ||
} | { | ||
body?: BodyInit | null | undefined; | ||
url: URL; | ||
cache?: RequestCache | undefined; | ||
credentials?: RequestCredentials | undefined; | ||
headers?: HeadersInit | undefined; | ||
integrity?: string | undefined; | ||
keepalive?: boolean | undefined; | ||
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS" | "HEAD" | "CONNECT" | "TRACE" | "get" | "post" | "put" | "patch" | "delete" | "options" | "head" | "connect" | "trace"; | ||
mode?: RequestMode | undefined; | ||
priority?: RequestPriority | undefined; | ||
redirect?: RequestRedirect | undefined; | ||
referrer?: string | undefined; | ||
referrerPolicy?: ReferrerPolicy | undefined; | ||
signal?: AbortSignal | null | undefined; | ||
window?: null | undefined; | ||
data?: unknown; | ||
params?: unknown; | ||
}; | ||
declare function response<M extends LowercaseMethods | Methods, D, P, RQ extends TypedRequestConfig<M, D, P> | URL | string | Request, R2 = Promise<Response>>(req: RQ, ctx: Context<string, Lowercase<M>, Args, string>): R2; | ||
declare function addParams<const U extends string>( | ||
req: TypedRequestConfig<LowercaseMethods | Methods, U> & { | ||
params?: Record<string, URICompoent> | ||
} | ||
): string | ||
declare function parseUrl<CTX extends Obj>( | ||
req: TypedRequestConfig<LowercaseMethods | Methods> | URL | string | Request, | ||
context?: CTX | ||
): URL | ||
declare function parseBody( | ||
req: TypedRequestConfig<LowercaseMethods | Methods> | URL | string | Request | ||
): RequestInit['body'] | ||
declare function parseRequest<M extends LowercaseMethods>( | ||
req: TypedRequestConfig<LowercaseMethods | Methods> | URL | string | Request, | ||
url: URL, | ||
body: RequestInit['body'], | ||
ctx: Context<string, M, Args, string> | ||
): | ||
| { | ||
body?: BodyInit | null | undefined | ||
url: URL | ||
method: M | ||
} | ||
| { | ||
body?: BodyInit | null | undefined | ||
url: URL | ||
hash: string | ||
host: string | ||
hostname: string | ||
href: string | ||
toString(): string | ||
origin: string | ||
password: string | ||
pathname: string | ||
port: string | ||
protocol: string | ||
search: string | ||
searchParams: URLSearchParams | ||
username: string | ||
toJSON(): string | ||
createObjectURL(obj: Blob): string | ||
revokeObjectURL(url: string): void | ||
canParse(url: string, base?: string | undefined): boolean | ||
method: M | ||
} | ||
| { | ||
body: BodyInit | null | ||
url: URL | ||
cache: RequestCache | ||
credentials: RequestCredentials | ||
destination: RequestDestination | ||
headers: Headers | ||
integrity: string | ||
keepalive: boolean | ||
method: string | ||
mode: RequestMode | ||
redirect: RequestRedirect | ||
referrer: string | ||
referrerPolicy: ReferrerPolicy | ||
signal: AbortSignal | ||
clone(): Request | ||
bodyUsed: boolean | ||
arrayBuffer(): Promise<ArrayBuffer> | ||
blob(): Promise<Blob> | ||
formData(): Promise<FormData> | ||
json(): Promise<any> | ||
text(): Promise<string> | ||
} | ||
| { | ||
body?: BodyInit | null | undefined | ||
url: URL | ||
cache?: RequestCache | undefined | ||
credentials?: RequestCredentials | undefined | ||
headers?: HeadersInit | undefined | ||
integrity?: string | undefined | ||
keepalive?: boolean | undefined | ||
method: | ||
| 'GET' | ||
| 'POST' | ||
| 'PUT' | ||
| 'PATCH' | ||
| 'DELETE' | ||
| 'OPTIONS' | ||
| 'HEAD' | ||
| 'CONNECT' | ||
| 'TRACE' | ||
| 'get' | ||
| 'post' | ||
| 'put' | ||
| 'patch' | ||
| 'delete' | ||
| 'options' | ||
| 'head' | ||
| 'connect' | ||
| 'trace' | ||
mode?: RequestMode | undefined | ||
priority?: RequestPriority | undefined | ||
redirect?: RequestRedirect | undefined | ||
referrer?: string | undefined | ||
referrerPolicy?: ReferrerPolicy | undefined | ||
signal?: AbortSignal | null | undefined | ||
window?: null | undefined | ||
data?: unknown | ||
checkParams?: true | undefined | ||
params?: Record<string, URICompoent> | undefined | ||
} | ||
declare function response< | ||
M extends LowercaseMethods | Methods, | ||
RQ extends TypedRequestConfig<M> | URL | string | Request, | ||
R2 = Promise<Response>, | ||
>(req: RQ, ctx: Context<string, Lowercase<M>, Args, string>): R2 | ||
export { type LowercaseMethods, type Methods, type RequestMap, type StaticConfig, type TypedRequestConfig, type URLType, parseBody, parseRequest, parseUrl, resolve, response }; | ||
declare function rq<const M extends LowercaseMethods | Methods, const U extends string, P extends boolean = true>( | ||
config: TypedRequestConfig<M, U, P> & { | ||
method: M | ||
} | ||
): Partial< | ||
RequestInit & { | ||
method?: M | undefined | ||
data?: unknown | ||
checkParams?: P | undefined | ||
params?: Record<string, URICompoent> | undefined | ||
} | ||
> & { | ||
readonly url: U | ||
} & (Uppercase<M> extends 'GET' | 'HEAD' | ||
? { | ||
body?: | ||
| _hulla_api.TypeError< | ||
`ERROR: method "${M}" cannot contain a body`, | ||
'https://developer.mozilla.org/en-US/docs/Web/API/Request/body' | ||
> | ||
| undefined | ||
} | ||
: {}) & | ||
(P extends true | ||
? U extends string | ||
? ( | ||
ExtractUrl<U> extends `${string}/:${infer Param}/${infer Rest}` | ||
? { [K in Param]: URICompoent } & ParsePath<Rest> | ||
: ExtractUrl<U> extends `${string}/:${infer Param_1}` | ||
? { [K_1 in Param_1]: URICompoent } | ||
: never | ||
) extends never | ||
? ParseSearch<U> extends never | ||
? {} | ||
: { | ||
params: URLToParams<U> | ||
} | ||
: { | ||
params: URLToParams<U> | ||
} | ||
: {} | ||
: {}) & { | ||
method: M | ||
} | ||
export { | ||
addParams, | ||
parseBody, | ||
parseRequest, | ||
parseUrl, | ||
resolve, | ||
response, | ||
rq, | ||
type Callables, | ||
type ExtractUrl, | ||
type LowercaseMethods, | ||
type Methods, | ||
type ParsePath, | ||
type ParseSearch, | ||
type RequestMap, | ||
type StaticConfig, | ||
type TypedRequestConfig, | ||
type URICompoent, | ||
type URLToParams, | ||
type URLType, | ||
} |
@@ -1,1 +0,49 @@ | ||
function t(t){return t instanceof URL}function e(e){return"string"==typeof e?new URL(e):t(e)?e:new URL(e.url)}function n(t){let e=t.data;return t.body?t.body:t.data?"string"==typeof e?e:JSON.stringify(e):void 0}function r(e,n,r,o){return{..."string"==typeof e||t(e)?{method:o.method}:{method:o.method,...e},url:n,...void 0===r?{}:{body:r}}}function o(t,o){let s=e(t),i=n(t),u=r(t,s,i,o);return fetch(s,u)}exports.parseBody=n,exports.parseRequest=r,exports.parseUrl=e,exports.resolve=function(t,e){return o(t,e).then(t=>t.json())},exports.response=o; | ||
function e(e) { | ||
return e instanceof URL | ||
} | ||
function t(e) { | ||
let t = e.url, | ||
n = e.params ?? {} | ||
for (let e in n) | ||
t = (t = (t = t.replace(RegExp(':' + e, 'g'), encodeURIComponent(n[e]))).replace( | ||
RegExp('\\?' + e, 'g'), | ||
`?${e}=${encodeURIComponent(n[e])}` | ||
)).replace(RegExp('&' + e, 'g'), `&${e}=${encodeURIComponent(n[e])}`) | ||
return t | ||
} | ||
function n(n, r = {}) { | ||
let o = r.baseURL ?? '' | ||
if ('string' == typeof n) return new URL(`${o}${n}`) | ||
if (e(n)) return n | ||
if (n instanceof Request) return new URL(n.url) | ||
let s = t(n) | ||
return new URL(`${o}${s}`) | ||
} | ||
function r(e) { | ||
let t = e.data | ||
return e.body ? e.body : e.data ? ('string' == typeof t ? t : JSON.stringify(t)) : void 0 | ||
} | ||
function o(t, n, r, o) { | ||
return { | ||
...('string' == typeof t || e(t) ? { method: o.method } : { method: o.method, ...t }), | ||
url: n, | ||
...(void 0 === r ? {} : { body: r }), | ||
} | ||
} | ||
function s(e, t) { | ||
let s = n(e, t), | ||
p = r(e), | ||
u = o(e, s, p, t) | ||
return fetch(s, u) | ||
} | ||
;(exports.addParams = t), | ||
(exports.parseBody = r), | ||
(exports.parseRequest = o), | ||
(exports.parseUrl = n), | ||
(exports.resolve = function (e, t, n) { | ||
return s(e, t).then((e) => e[n ?? 'json']()) | ||
}), | ||
(exports.response = s), | ||
(exports.rq = function (e) { | ||
return e | ||
}) |
{ | ||
"name": "@hulla/api-request", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "The next-gen API/RPC manager 🚀", | ||
@@ -37,5 +37,2 @@ "author": { | ||
}, | ||
"peerDependencies": { | ||
"@hulla/api": "1.0.0" | ||
}, | ||
"keywords": [ | ||
@@ -70,4 +67,5 @@ "hulla", | ||
"format": "prettier --write .", | ||
"test": "vitest run" | ||
"test": "vitest run", | ||
"clean": "rm -rf node_modules dist" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
21427
0
414
3