Back to Groups
Development

TypeScript: API integration engineer

TypeScript 5.8+, ES2022 and DOM types. Validate API responses, follow cursor pages, and prepare conditional requests. Review names and integration points before use.

type-safetyworkflowstypescripttypescript-developercode
SA
samriver
0likes12snippets

Try it here

Loading snippet…

Your typing stays in this tab. Nothing is saved or sent.
Snippets (12)
;typ-bounded-int
Parse a whole-number setting
function parseBoundedInt(raw: string, min: number, max: number): number { if (!Number.isSafeIntege
;typ-json-record
Parse a bounded JSON object
function parseJsonRecord(text: string): Record<string, unknown> { if (text.length > 100_000) throw
;typ-exhaustive
Exhaustive discriminated-union switch
type LoadState = | { kind: "idle" } | { kind: "loading" } | { kind: "ready"; count: number }
;typ-group-by
Group records without object-key collisions
function groupBy<T, K>(items: Iterable<T>, keyOf: (item: T) => K): Map<K, T[]> { const groups = ne
;typ-unique-by
Keep the first record for each key
function uniqueBy<T, K>(items: Iterable<T>, keyOf: (item: T) => K): T[] { const seen = new Set<K>(
;typ-settled
Separate fulfilled values from rejected reasons
function partitionSettled<T>(results: readonly PromiseSettledResult<T>[]): { values: T[]; errors
;typ-page-envelope
Decode a cursor page at the API boundary
function decodeCursorPage<T>( input: unknown, decodeItem: (item: unknown) => T, ): { items: T[];
;typ-emitter
Typed synchronous subscription with cleanup
function createEmitter<T>() { const listeners = new Set<(value: T) => void>(); return { subs
;typ-attempt
Return an explicit async success or failure
type Attempt<T> = | { ok: true; value: T } | { ok: false; error: unknown }; async function atte
;typ-retry-after
Read Retry-After without retrying too early
function retryAfterMs( header: string | null, nowMs = Date.now(), maxWaitMs = 60_000, ): numbe
;typ-if-match
Require a strong ETag for conditional updates
function ifMatchHeaders(etag: string): Record<string, string> { // Accept one quoted ASCII strong
;typ-cursor-pages
Collect pages with cursor and item limits
async function collectCursorPages<T>( fetchPage: (cursor: string | null, signal: AbortSignal) =>