import {z} from 'zod' export type GetApiV1Args = {token: string, tag?: ('ai' | 'analyst' | 'backend' | 'bitcoin' | 'blockchain' | 'community-manager' | 'crypto' | 'cryptography' | 'cto' | 'customer-support' | 'dao' | 'data-science' | 'defi' | 'design' | 'developer-relations' | 'devops' | 'discord' | 'economy-designer' | 'entry-level' | 'erc' | 'erc-20' | 'evm' | 'front-end' | 'full-stack' | 'gaming' | 'ganache' | 'golang' | 'hardhat' | 'intern' | 'java' | 'javascript' | 'layer-2' | 'marketing' | 'mobile' | 'moderator' | 'nft' | 'node' | 'non-tech' | 'open-source' | 'openzeppelin' | 'pay-in-crypto' | 'product-manager' | 'project-manager' | 'react' | 'refi' | 'research' | 'ruby' | 'rust' | 'sales' | 'smart-contract' | 'solana' | 'solidity' | 'truffle' | 'web3-py' | 'web3js' | 'zero-knowledge') | undefined, country?: string | undefined, remote?: 'true' | undefined, limit?: number | undefined, show_description?: ('true' | 'false') | undefined}; export type GetApiV1Options = {baseUrl?: string | undefined, apiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const job = z.object({id: z.string().optional(), title: z.string().optional(), company: z.string().optional(), location: z.string().optional(), remote: z.boolean().optional(), description: z.string().optional(), tags: z.array(z.string()).optional(), apply_url: z.string().optional(), url: z.string().optional(), salary: z.string().optional(), postedAt: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const apiResponse = z.array(z.union([z.string(), z.array(job)])); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get Web3 job listings (JSON) * * GET /v1 */ export const getApiV1 = async (args: GetApiV1Args, options?: GetApiV1Options) => { const {baseUrl = "https://web3.career/api", fetch: fetchFn = fetch, apiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.token != null) query.append("token", String(args.token)) if (args.tag != null) query.append("tag", String(args.tag)) if (args.country != null) query.append("country", String(args.country)) if (args.remote != null) query.append("remote", String(args.remote)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.show_description != null) query.append("show_description", String(args.show_description)) if (apiToken !== undefined) query.set("token", apiToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(apiResponse, await res.json()) };