{"id":21149048,"url":"https://github.com/jedrzejginter/endpoints","last_synced_at":"2025-03-14T14:14:32.983Z","repository":{"id":52872355,"uuid":"354332614","full_name":"jedrzejginter/endpoints","owner":"jedrzejginter","description":"Useful TypeScript types and utils for axios fetching","archived":false,"fork":false,"pushed_at":"2021-04-15T16:41:08.000Z","size":61,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-21T07:44:20.781Z","etag":null,"topics":["axios","endpoints","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/jedrzejginter.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}},"created_at":"2021-04-03T15:55:21.000Z","updated_at":"2021-10-11T16:43:17.000Z","dependencies_parsed_at":"2022-08-23T10:40:38.366Z","dependency_job_id":null,"html_url":"https://github.com/jedrzejginter/endpoints","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedrzejginter%2Fendpoints","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedrzejginter%2Fendpoints/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedrzejginter%2Fendpoints/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedrzejginter%2Fendpoints/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jedrzejginter","download_url":"https://codeload.github.com/jedrzejginter/endpoints/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243589330,"owners_count":20315471,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["axios","endpoints","typescript"],"created_at":"2024-11-20T09:30:52.945Z","updated_at":"2025-03-14T14:14:32.962Z","avatar_url":"https://github.com/jedrzejginter.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @ginterdev/endpoints\n\n\u003e Useful TypeScript types and utils for axios fetching\n\n## Warning :warning:\n\n**For now I am just experimenting.**\n\nThis package makes some assumptions:\n\n- you are using **axios** (^0.21)\n\n## Installation\n\n```bash\nyarn add @ginterdev/endpoints\n\n# or\n\nnpm i @ginterdev/endpoints\n```\n\n## Examples\n\nLet's define types for our API. For simplicity there's two endpoints only.\n\n```ts\ntype Endpoints {\n  'POST /companies': {\n    body: {\n      name: string;\n    };\n    response: {\n      success: boolean;\n    };\n  };\n\n  'GET /companies': {\n    query?: {\n      searchPhrase?: string;\n    };\n    response: {\n      success: boolean,\n      companies: {\n        id: string;\n        name: string;\n      };\n    };\n  };\n\n  'GET /company/{id}': {\n    params: {\n      id: string;\n    };\n    response: {\n      success: boolean,\n      company: {\n        id: string;\n        name: string;\n      };\n    };\n  };\n};\n```\n\n### Calling a request\n\nTo have all the typechecking in place for our endpoints we just wrap a fetcher (currently only `axios` instance is supported) with a `forEndpoints`. From now on we can execute requests with a guarantee that all required things are passed correctly.\n\n```ts\nimport axios from 'axios';\nimport { forEndpoints } from '@ginterdev/endpoints';\n\nconst request = forEndpoints\u003cEndpoints\u003e(axios.create());\n\n// execute a request:\nrequest('GET /companies', {\n  query: {\n    searchPhrase: 'github',\n  },\n});\n\n// URL params are automatically injected:\nrequest('GET /companies/{id}', {\n  params: {\n    id: 'foobar',\n  },\n});\n```\n\n### Extracting types\n\nNow we can easily extract types of specific properties for each endpoint:\n\n\u003c!-- prettier-ignore --\u003e\n```ts\nimport { EndpointOptions, EndpointProp } from '@ginterdev/endpoints';\n\ntype EndpointUrl = keyof Endpoints;\n\ntype Options\u003cUrl extends EndpointUrl\u003e = EndpointOptions\u003cEndpoints, Url\u003e;\ntype Body\u003cUrl extends EndpointUrl\u003e = EndpointProp\u003cEndpoints, Url, 'body'\u003e;\ntype Params\u003cUrl extends EndpointUrl\u003e = EndpointProp\u003cEndpoints, Url, 'params'\u003e;\ntype Query\u003cUrl extends EndpointUrl\u003e = EndpointProp\u003cEndpoints, Url, 'query'\u003e;\ntype Response\u003cUrl extends EndpointUrl\u003e = EndpointProp\u003cEndpoints, Url, 'response'\u003e;\n\ntype CreateCompanyBody = Body\u003c'POST /companies'\u003e;\n// { name: string }\n\ntype CreateCompanyResponse = Response\u003c'POST /companies'\u003e;\n// { success: true }\n\ntype GetCompaniesQuery = Query\u003c'GET /companies'\u003e;\n// { searchPhrase?: string }\n\ntype GetCompanyParams = Params\u003c'GET /companies/{id}'\u003e;\n// { id: string }\n```\n\n\u003c!-- prettier-ignore --\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjedrzejginter%2Fendpoints","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjedrzejginter%2Fendpoints","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjedrzejginter%2Fendpoints/lists"}