2024-05-17 01:38:36 +00:00
|
|
|
import type * as types from './types.js'
|
|
|
|
|
2024-06-03 06:19:10 +00:00
|
|
|
export { assert } from './assert.js'
|
2024-05-17 01:38:36 +00:00
|
|
|
export { default as delay } from 'delay'
|
2024-05-17 01:22:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* From `inputObj`, create a new object that does not include `keys`.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```js
|
|
|
|
* omit({ a: 1, b: 2, c: 3 }, 'a', 'c') // { b: 2 }
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export const omit = <
|
2024-05-26 20:41:18 +00:00
|
|
|
T extends Record<any, unknown> | object,
|
2024-05-17 01:22:07 +00:00
|
|
|
K extends keyof T = keyof T
|
|
|
|
>(
|
|
|
|
inputObj: T,
|
|
|
|
...keys: K[]
|
|
|
|
): Omit<T, K> => {
|
|
|
|
const keysSet = new Set(keys)
|
|
|
|
return Object.fromEntries(
|
|
|
|
Object.entries(inputObj).filter(([k]) => !keysSet.has(k as any))
|
|
|
|
) as any
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* From `inputObj`, create a new object that only includes `keys`.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```js
|
|
|
|
* pick({ a: 1, b: 2, c: 3 }, 'a', 'c') // { a: 1, c: 3 }
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export const pick = <
|
2024-05-26 20:41:18 +00:00
|
|
|
T extends Record<any, unknown> | object,
|
2024-05-17 01:22:07 +00:00
|
|
|
K extends keyof T = keyof T
|
|
|
|
>(
|
|
|
|
inputObj: T,
|
|
|
|
...keys: K[]
|
|
|
|
): Pick<T, K> => {
|
|
|
|
const keysSet = new Set(keys)
|
|
|
|
return Object.fromEntries(
|
|
|
|
Object.entries(inputObj).filter(([k]) => keysSet.has(k as any))
|
|
|
|
) as any
|
|
|
|
}
|
|
|
|
|
|
|
|
export function pruneUndefined<T extends Record<string, any>>(
|
|
|
|
obj: T
|
2024-05-26 20:41:18 +00:00
|
|
|
): NonNullable<{ [K in keyof T]: Exclude<T[K], undefined> }> {
|
2024-05-17 01:22:07 +00:00
|
|
|
return Object.fromEntries(
|
|
|
|
Object.entries(obj).filter(([, value]) => value !== undefined)
|
|
|
|
) as NonNullable<T>
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getEnv(name: string): string | undefined {
|
|
|
|
try {
|
|
|
|
return typeof process !== 'undefined'
|
|
|
|
? // eslint-disable-next-line no-process-env
|
|
|
|
process.env?.[name]
|
|
|
|
: undefined
|
|
|
|
} catch {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|
2024-05-17 01:38:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function that does nothing.
|
|
|
|
*/
|
|
|
|
export const noop = () => undefined
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throttles HTTP requests made by a ky instance.
|
|
|
|
*
|
|
|
|
* Very useful for enforcing rate limits.
|
|
|
|
*/
|
|
|
|
export function throttleKy(
|
|
|
|
ky: types.KyInstance,
|
|
|
|
throttleFn: <Arguments extends readonly unknown[], ReturnValue>(
|
|
|
|
function_: (...args_: Arguments) => ReturnValue
|
|
|
|
) => types.ThrottledFunction<(...args_: Arguments) => ReturnValue>
|
|
|
|
) {
|
|
|
|
return ky.extend({
|
|
|
|
hooks: {
|
|
|
|
beforeRequest: [throttleFn(noop)]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|