UNPKG

@hazae41/glacier

Version:

Yet another React data (re)fetching library

1 lines 8.55 kB
{"version":3,"file":"index.mjs","sources":["../../../../../src/mods/requests/index.ts"],"sourcesContent":["export interface RequestLike extends RequestInit {\n url: string\n destination: RequestDestination\n}\n\nexport interface EmptyRequestInit extends Omit<RequestInit, \"body\"> {\n /**\n * Empty\n */\n}\n\nexport namespace EmptyRequest {\n\n /**\n * Hack to only allow EmptyRequest or Request-like but not any other variant\n */\n export interface From extends RequestLike {\n bodyAsText?: undefined\n bodyAsJson?: undefined\n }\n\n}\n\nexport class EmptyRequest extends Request {\n\n #headers: Record<string, string> = {}\n\n constructor(input: RequestInfo, init?: EmptyRequestInit) {\n super(input, init)\n\n this.headers.forEach((value, key) => this.#headers[key] = value)\n\n return\n }\n\n static async from(from: EmptyRequest.From): Promise<EmptyRequest> {\n if (from instanceof EmptyRequest)\n return from\n\n const request = new Request(from.url, from)\n\n return new EmptyRequest(request)\n }\n\n get headersAsJson(): Record<string, string> {\n return this.#headers\n }\n\n toJSON(): RequestLike {\n return {\n url: this.url,\n method: this.method,\n headers: this.headersAsJson,\n keepalive: this.keepalive,\n cache: this.cache,\n credentials: this.credentials,\n destination: this.destination,\n integrity: this.integrity,\n mode: this.mode,\n redirect: this.redirect,\n referrerPolicy: this.referrerPolicy\n } as const\n }\n\n}\n\nexport interface TextRequestInit extends Omit<RequestInit, \"body\"> {\n body: string\n}\n\nexport namespace TextRequest {\n\n /**\n * Hack to only allow TextRequest or Request-like but not any other variant\n */\n export interface From extends RequestLike {\n bodyAsText?: string\n bodyAsJson?: undefined\n }\n\n}\n\nexport class TextRequest extends Request {\n\n #headers: Record<string, string> = {}\n\n #bodyAsText: string\n\n constructor(input: RequestInfo, init: TextRequestInit) {\n super(input, init)\n\n this.headers.forEach((value, key) => this.#headers[key] = value)\n\n this.#bodyAsText = init.body\n }\n\n static async from(from: TextRequest.From): Promise<TextRequest> {\n if (from instanceof TextRequest)\n return from\n\n const request = new Request(from.url, from)\n\n const body = await request.text()\n\n return new TextRequest(request, { body })\n }\n\n get headersAsJson(): Record<string, string> {\n return this.#headers\n }\n\n get bodyAsText(): string {\n return this.#bodyAsText\n }\n\n toJSON(): RequestLike {\n return {\n url: this.url,\n method: this.method,\n headers: this.headersAsJson,\n body: this.bodyAsText,\n keepalive: this.keepalive,\n cache: this.cache,\n credentials: this.credentials,\n destination: this.destination,\n integrity: this.integrity,\n mode: this.mode,\n redirect: this.redirect,\n referrerPolicy: this.referrerPolicy,\n } as const\n }\n\n}\n\nexport interface JsonRequestInit<T> extends Omit<RequestInit, \"body\"> {\n body: T\n}\n\nexport namespace JsonRequest {\n\n /**\n * Hack to only allow JsonRequest<T> or Request-like but not any other variant\n */\n export interface From<T> extends RequestLike {\n bodyAsText?: string\n bodyAsJson?: T\n }\n\n}\n\nexport class JsonRequest<T> extends Request {\n\n #headers: Record<string, string> = {}\n\n #bodyAsJson: T\n #bodyAsText: string\n\n constructor(input: RequestInfo, init: JsonRequestInit<T>) {\n const body = JSON.stringify(init.body)\n\n super(input, { ...init, body })\n\n if (!this.headers.get(\"Content-Type\")?.includes(\"application/json\"))\n this.headers.set(\"Content-Type\", \"application/json\")\n\n this.headers.forEach((value, key) => this.#headers[key] = value)\n\n this.#bodyAsJson = init.body\n this.#bodyAsText = body\n }\n\n static async from<T>(from: JsonRequest.From<T>): Promise<JsonRequest<T>> {\n if (from instanceof JsonRequest)\n return from\n\n const request = new Request(from.url, from)\n\n const body = await request.json() as T\n\n return new JsonRequest<T>(request, { body })\n }\n\n get headersAsJson(): Record<string, string> {\n return this.#headers\n }\n\n get bodyAsJson(): T {\n return this.#bodyAsJson\n }\n\n get bodyAsText(): string {\n return this.#bodyAsText\n }\n\n toJSON(): RequestLike {\n return {\n url: this.url,\n method: this.method,\n headers: this.headersAsJson,\n body: this.bodyAsText,\n keepalive: this.keepalive,\n cache: this.cache,\n credentials: this.credentials,\n destination: this.destination,\n integrity: this.integrity,\n mode: this.mode,\n redirect: this.redirect,\n referrerPol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