utils.ts
This file
Path
src/client/utils.ts
114 lines · 3.3 KiB · TypeScript
Last changed by
chore: bump linter and formatter (#4568)ryubefore adoptionb6f4bcd
History of this fileTake it
The walk stopped after 200 commits: lines older than 82bdc4d point at it.
fix(parseResponse): should not include error responses in result (#4348)Trung Dangbefore adoption08a80e2
lines 1–5why, one step back1import type {2 ClientErrorStatusCode,3 ContentfulStatusCode,4 ServerErrorStatusCode,5} from '../utils/http-status'4.9.0Yusuke Wadabefore adoption82bdc4d
line 6why, one step back6import { fetchRP, DetailedError } from './fetch-result-please'fix(parseResponse): should not include error responses in result (#4348)Trung Dangbefore adoption08a80e2
line 7why, one step back7import type { ClientResponse, FilterClientResponseByStatusCode, ObjectType } from './types'4.9.0Yusuke Wadabefore adoption82bdc4d
lines 8–54why, one step back89export { DetailedError }1011export const mergePath = (base: string, path: string) => {12 base = base.replace(/\/+$/, '')13 base = base + '/'14 path = path.replace(/^\/+/, '')15 return base + path16}1718export const replaceUrlParam = (urlString: string, params: Record<string, string | undefined>) => {19 for (const [k, v] of Object.entries(params)) {20 const reg = new RegExp('/:' + k + '(?:{[^/]+})?\\??')21 urlString = urlString.replace(reg, v ? `/${v}` : '')22 }23 return urlString24}2526export const buildSearchParams = (query: Record<string, string | string[]>) => {27 const searchParams = new URLSearchParams()2829 for (const [k, v] of Object.entries(query)) {30 if (v === undefined) {31 continue32 }3334 if (Array.isArray(v)) {35 for (const v2 of v) {36 searchParams.append(k, v2)37 }38 } else {39 searchParams.set(k, v)40 }41 }4243 return searchParams44}4546export const replaceUrlProtocol = (urlString: string, protocol: 'ws' | 'http') => {47 switch (protocol) {48 case 'ws':49 return urlString.replace(/^http/, 'ws')50 case 'http':51 return urlString.replace(/^ws/, 'http')52 }53}54fix(client): handle query parameters in `removeIndexString` (#4352)Yusuke Wadabefore adoption6b19b5e
lines 55–57why, one step back55export const removeIndexString = (urlString: string) => {56 if (/^https?:\/\/[^\/]+?\/index(?=\?|$)/.test(urlString)) {57 return urlString.replace(/\/index(?=\?|$)/, '/')4.9.0Yusuke Wadabefore adoption82bdc4d
line 58why, one step back58 }fix(client): handle query parameters in `removeIndexString` (#4352)Yusuke Wadabefore adoption6b19b5e
line 59why, one step back59 return urlString.replace(/\/index(?=\?|$)/, '')4.9.0Yusuke Wadabefore adoption82bdc4d
lines 60–94why, one step back60}6162function isObject(item: unknown): item is ObjectType {63 return typeof item === 'object' && item !== null && !Array.isArray(item)64}6566export function deepMerge<T>(target: T, source: Record<string, unknown>): T {67 if (!isObject(target) && !isObject(source)) {68 return source as T69 }70 const merged = { ...target } as ObjectType<T>7172 for (const key in source) {73 const value = source[key]74 if (isObject(merged[key]) && isObject(value)) {75 merged[key] = deepMerge(merged[key], value)76 } else {77 merged[key] = value as T[keyof T] & T78 }79 }8081 return merged as T82}8384/**85 * Shortcut to get a consumable response from `hc`'s fetch calls (Response), with types inference.86 *87 * Smartly parse the response data, throwing a structured error if the response is not `ok`. ({@link DetailedError})88 *89 * @example const result = await parseResponse(client.posts.$get())90 */91// eslint-disable-next-line @typescript-eslint/no-explicit-any92export async function parseResponse<T extends ClientResponse<any>>(93 fetchRes: T | Promise<T>94): Promise<fix(parseResponse): should not include error responses in result (#4348)Trung Dangbefore adoption08a80e2
lines 95–101why, one step back95 FilterClientResponseByStatusCode<96 T,97 Exclude<ContentfulStatusCode, ClientErrorStatusCode | ServerErrorStatusCode> // Filter out the error responses98 > extends never99 ? // Filtered responses does not include any contentful responses, exit with undefined100 undefined101 : // Filtered responses includes contentful responses, proceed to infer the typechore: bump linter and formatter (#4568)ryubefore adoptionb6f4bcd
lines 102–111why, one step back102 FilterClientResponseByStatusCode<103 T,104 Exclude<ContentfulStatusCode, ClientErrorStatusCode | ServerErrorStatusCode>105 > extends ClientResponse<infer RT, infer _, infer RF>106 ? RF extends 'json'107 ? RT108 : RT extends string109 ? RT110 : string111 : undefined4.9.0Yusuke Wadabefore adoption82bdc4d
lines 112–114why, one step back112> {113 return fetchRP(fetchRes)114}