{"id":43270552,"url":"https://github.com/treeder/api","last_synced_at":"2026-02-01T15:41:28.230Z","repository":{"id":199560230,"uuid":"703196419","full_name":"treeder/api","owner":"treeder","description":"Fetch with auth and JSON as default.","archived":false,"fork":false,"pushed_at":"2025-09-12T15:04:41.000Z","size":85,"stargazers_count":3,"open_issues_count":6,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-09-12T16:57:57.245Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/treeder.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-10-10T19:22:14.000Z","updated_at":"2025-09-12T15:04:45.000Z","dependencies_parsed_at":"2023-10-11T00:31:17.424Z","dependency_job_id":"dd85724b-b73d-4770-b80f-a2b71107e6d7","html_url":"https://github.com/treeder/api","commit_stats":null,"previous_names":["treeder/api"],"tags_count":46,"template":false,"template_full_name":null,"purl":"pkg:github/treeder/api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/treeder%2Fapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/treeder%2Fapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/treeder%2Fapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/treeder%2Fapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/treeder","download_url":"https://codeload.github.com/treeder/api/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/treeder%2Fapi/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28981454,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-01T15:35:50.179Z","status":"ssl_error","status_checked_at":"2026-02-01T15:35:38.075Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-02-01T15:41:28.096Z","updated_at":"2026-02-01T15:41:28.214Z","avatar_url":"https://github.com/treeder.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# api function - an enhanced fetch\n\nThis package assumes your using a REST API with JSON requests and responses. And if you are, it will handle\nsetting headers for content-type and authorization, serialing and parsing JSON and some other nice tricks\nlike caching responses.\n\n## Getting started\n\n### On the server\n\n```sh\nnpm install treeder/api\n```\n\nThen import:\n\n```js\nimport { api, API, apiInit } from 'api'\n```\n\n### In the browser\n\n```js\nimport { api, API, apiInit } from 'https://cdn.jsdelivr.net/gh/treeder/api@1/api.js'\n```\n\n- api is a generic API instance, ready to use just by calling `api(url, opts)` instead of `fetch(url, opts)`\n- API is a class letting you instantiate and customize.\n- apiInit lets you customize the default API instance.\n\n## Usage\n\nThen just use it in place of fetch:\n\n```js\nlet r = await api(`/v1/users`)\n```\n\nUse with [models](https://github.com/treeder/models) package to get even better JSON parsing capabilities\n\n```js\nlet r = await api(`/v1/user/123`)\nlet user = parseModel(r.user, User)\n```\n\n## Configure default instance\n\n```js\napiInit({ apiURL: apiURL })\n```\n\n- apiURL will be prefixed to any calls.\n\n### Authentication\n\nAdd headers to the API instance that will be used for all subsequent calls.\n\n```js\napiInit({ headers: { Authorization: `apiKey ${process.env.API_KEY}` } })\n```\n\n## Multiple APIs\n\nIf you have different API's you are talking to, you can create new instances:\n\n```js\nconst api2 = new API({ apiURL: 'https://somewhere.com' })\n// then use it with:\nlet r = await api2.fetch('/abc')\n```\n\n## Errors\n\nAny errors will throw an `APIError` which has a `status` field on it to check the code.\n\n```js\ntry {\n  let r = await api('https://x.com/abc')\n} catch (e) {\n  return Response.json({ error: e }, { status: e.status })\n}\n```\n\nThis is exported too so you can use it in your API's to return nice errors.\n\n## Caching\n\nCaching can really help reduce network traffic and increase performance\ndepending on your use case. This will only cache GET requests.\n\nTo use caching, you have to create an api object and use `fetchAndCache`.\n\n```js\nlet api = new API()\nlet r = await api.fetchAndCache('https://somewhere.com/my/stuff')\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftreeder%2Fapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftreeder%2Fapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftreeder%2Fapi/lists"}