@soundify/shared
Advanced tools
Comparing version 0.1.0-alpha.3 to 0.1.1-beta.0
/** | ||
* Creates URLSearchParams from object | ||
* | ||
* You can use `new URLSearchParams(obj)`, when your object | ||
* extends `Record<string, string>`. Otherwise use this function. | ||
* Creates a query string from the object and skips `undefined`. | ||
*/ | ||
export const objectToSearchParams = (obj) => { | ||
export const toQueryString = (obj) => { | ||
const params = new URLSearchParams(); | ||
Object.keys(obj).forEach((key) => { | ||
const value = obj[key]; | ||
if (typeof value === "undefined") | ||
return; | ||
params.set(key, value.toString()); | ||
}); | ||
return params; | ||
for (const [name, value] of Object.entries(obj)) { | ||
if (typeof value !== "undefined") | ||
params.set(name, value.toString()); | ||
} | ||
return params.toString(); | ||
}; |
@@ -5,8 +5,5 @@ { | ||
"name": "@soundify/shared", | ||
"version": "0.1.0-alpha.3", | ||
"version": "0.1.1-beta.0", | ||
"description": "⚙️ Shared types and functions for soundify packages", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/node": "^18.11.9" | ||
}, | ||
"packageManager": "pnpm@7.30.0", | ||
@@ -13,0 +10,0 @@ "repository": { |
@@ -1,43 +0,10 @@ | ||
export type SearchParam = string | number | boolean | undefined | string[]; | ||
export type SearchParams = Record<string, SearchParam>; | ||
/** | ||
* Creates URLSearchParams from object | ||
* | ||
* You can use `new URLSearchParams(obj)`, when your object | ||
* extends `Record<string, string>`. Otherwise use this function. | ||
*/ | ||
export declare const objectToSearchParams: <T extends SearchParams>(obj: T) => URLSearchParams; | ||
export type JSONValue = null | string | number | boolean | JSONObject | JSONArray; | ||
export type JSONArray = JSONValue[]; | ||
export interface JSONObject { | ||
[x: string]: JSONValue | undefined; | ||
} | ||
export type NonNullableJSON<T extends JSONObject> = { | ||
[K in keyof T]: NonNullable<T[K]>; | ||
export type SearchParam = string | number | boolean | SearchParamArray; | ||
export type SearchParamArray = SearchParam[]; | ||
export type SearchParams = { | ||
[k: string]: SearchParam | undefined; | ||
}; | ||
export type HTTPMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; | ||
export interface FetchOpts { | ||
method?: HTTPMethod; | ||
body?: JSONValue; | ||
query?: SearchParams; | ||
headers?: HeadersInit; | ||
} | ||
export type ExpectedResponse = "json" | "void"; | ||
/** | ||
* Interface that provides a fetch method to make HTTP requests. | ||
* Creates a query string from the object and skips `undefined`. | ||
*/ | ||
export interface HTTPClient { | ||
/** | ||
* Sends an HTTP request. | ||
* | ||
* @param baseURL | ||
* The base URL for the API request. Must begin with "/" | ||
* @param returnType | ||
* The expected return type of the API response. | ||
* @param opts | ||
* Optional request options, such as the request body or query parameters. | ||
*/ | ||
fetch(baseURL: string, responseType: "void", opts?: FetchOpts): Promise<void>; | ||
fetch<R extends JSONValue = JSONValue>(baseURL: string, responseType: "json", opts?: FetchOpts): Promise<R>; | ||
} | ||
export declare const toQueryString: <T extends SearchParams>(obj: T) => string; | ||
/** | ||
@@ -47,9 +14,4 @@ * The interface used to provide access token with the ability to refresh it | ||
export interface IAuthProvider { | ||
/** | ||
* Function that gives you access token. | ||
* | ||
* @param forceRefresh Does the service have to refresh the token, or give you a cached token | ||
* @default false | ||
*/ | ||
getAccessToken: (forceRefresh?: boolean) => Promise<string>; | ||
refreshToken(): Promise<string>; | ||
getToken(): string; | ||
} |
0
3161
27