{"id":25566524,"url":"https://github.com/pdmlab/http-problem-details-parser","last_synced_at":"2025-04-12T10:51:27.403Z","repository":{"id":40615862,"uuid":"359261233","full_name":"PDMLab/http-problem-details-parser","owner":"PDMLab","description":"Parse HTTP Problem Details (RFC7807) from objects / JSON to your local error types.","archived":false,"fork":false,"pushed_at":"2023-07-19T09:18:19.000Z","size":664,"stargazers_count":20,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-26T05:41:54.507Z","etag":null,"topics":["api","client","error-handling","rfc-7807","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/http-problem-details-parser","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/PDMLab.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2021-04-18T21:59:09.000Z","updated_at":"2024-08-07T04:31:49.000Z","dependencies_parsed_at":"2025-02-20T22:33:07.830Z","dependency_job_id":"6fa4ad55-5ba9-4444-b928-7007c016cc1c","html_url":"https://github.com/PDMLab/http-problem-details-parser","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fhttp-problem-details-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fhttp-problem-details-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fhttp-problem-details-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fhttp-problem-details-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PDMLab","download_url":"https://codeload.github.com/PDMLab/http-problem-details-parser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248557844,"owners_count":21124165,"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":["api","client","error-handling","rfc-7807","typescript"],"created_at":"2025-02-20T22:33:01.497Z","updated_at":"2025-04-12T10:51:27.384Z","avatar_url":"https://github.com/PDMLab.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# http-problem-details-parser\n\nBased on `http-problem-details` ([repository](https://github.com/PDMLab/http-problem-details) | [npm](https://www.npmjs.com/package/http-problem-details)) , this library allows you to map your HTTP problem details according to [RFC7807](https://tools.ietf.org/html/rfc7807) by convention in a JavaScript / TypeScript client.\n\n## Installation\n\n```bash\nnpm install http-problem-details-parser\n```\n\nor\n\n```bash\nyarn add http-problem-details-parser\n```\n\n## Usage\n\nGiven have a HTTP Problem respons like this:\n\n```json\n{\n  \"type\": \"https://example.net/not-found\",\n  \"status\": 404,\n  \"title\": \"Customer not found.\"\n}\n```\n\nYou can now simply parse it to a `ProblemDocument` document like this:\n\n```typescript\nimport { fromJSON } from 'http-problem-details-parser'\nconst result = fromJSON(status400JSON)\n```\n\n`http-problem-details-parser` also supports mapping extensions. Given you have this response:\n\n```json\n{\n  \"type\": \"https://example.net/validation-error\",\n  \"status\": 400,\n  \"title\": \"Your request parameters didn't validate.\",\n  \"instance\": \"https://example.net/account/logs/123\",\n  \"invalid-params\": [\n    {\n      \"name\": \"age\",\n      \"reason\": \"must be a positive integer\"\n    },\n    {\n      \"name\": \"color\",\n      \"reason\": \"must be 'green', 'red' or 'blue'\"\n    }\n  ]\n}\n```\n\nYou can now specify a mapper to map the extension `invalid-params`:\n\n```typescript\nconst mappers: HttpProblemExtensionMapper[] = [\n  {\n    type: 'https://example.net/validation-error',\n    map: (object: any) =\u003e\n      new ProblemDocumentExtension({\n        'invalid-params': object['invalid-params']\n      })\n  }\n]\n```\n\nwhen calling `fromJSON(status400JSON, mappers)`, the `invalid-params` extension gets parsed as well.\n\nIf you want to handle the `ProblemDocument` instances created above later on, it is recommended to create types for them.\n\n```typescript\ntype ValidationProblemDocument = Override\u003c\n  ProblemDocument,\n  {\n    type: 'https://example.net/validation-error'\n    'invalid-params': {\n      name: string\n      reason: string\n    }[]\n  }\n\u003e\n\ntype NotFoundProblemDocument = Override\u003c\n  ProblemDocument,\n  {\n    status: 404\n    type: 'https://example.net/not-found'\n  }\n\u003e\n\ntype Problem = ValidationProblemDocument | NotFoundProblemDocument\n```\n\nNow you can use type guards or exhaustive pattern matching in TypeScript like this:\n\n```typescript\n// type guard\nfunction isValidationProblemDocument(\n  value: unknown\n): value is ValidationProblemDocument {\n  const x = value as ProblemDocument\n  return x.type === 'https://example.net/validation-error'\n}\n\n// pattern matching\nconst document = fromObject(status404, mappers) as Problem\nswitch (document.type) {\n  case 'https://example.net/validation-error':\n    document['invalid-params'].length.should.equal(2)\n    break\n  case 'https://example.net/not-found':\n    should.not.exist(document['invalid-params'])\n    break\n\n  default:\n    break\n}\n```\n\nIn addition, you get code completion support for your editor.\n\n## Running the tests\n\n```bash\nnpm test\n```\n\nor\n\n```bash\nyarn test\n```\n\n## Want to help?\n\nThis project is just getting off the ground and could use some help with cleaning things up and refactoring.\n\nIf you want to contribute - we'd love it! Just open an issue to work against so you get full credit for your fork. You can open the issue first so we can discuss and you can work your fork as we go along.\n\nIf you see a bug, please be so kind as to show how it's failing, and we'll do our best to get it fixed quickly.\n\nBefore sending a PR, please [create an issue](https://github.com/PDMLab/http-problem-details-parser/issues/new) to introduce your idea and have a reference for your PR.\n\nWe're using [conventional commits](https://www.conventionalcommits.org), so please use it for your commits as well.\n\nAlso please add tests and make sure to run `npm run lint-ts` or `yarn lint-ts`.\n\n## License\n\nMIT License\n\nCopyright (c) 2021 PDMLab\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdmlab%2Fhttp-problem-details-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpdmlab%2Fhttp-problem-details-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdmlab%2Fhttp-problem-details-parser/lists"}