{"id":15047014,"url":"https://github.com/nfroidure/schema2dts","last_synced_at":"2025-10-29T21:30:43.807Z","repository":{"id":39998779,"uuid":"291741148","full_name":"nfroidure/schema2dts","owner":"nfroidure","description":"A very simple JSONSchema to TypeScript types generator","archived":false,"fork":false,"pushed_at":"2024-09-24T13:51:19.000Z","size":1320,"stargazers_count":6,"open_issues_count":3,"forks_count":3,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-09-29T08:41:35.857Z","etag":null,"topics":["hacktoberfest","jsonschema","openapi3"],"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/nfroidure.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","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},"funding":{"github":["nfroidure"]}},"created_at":"2020-08-31T14:41:10.000Z","updated_at":"2024-07-18T13:49:02.000Z","dependencies_parsed_at":"2024-06-19T06:17:19.355Z","dependency_job_id":"21e95b53-a464-429a-a9e3-523fe1b4c59d","html_url":"https://github.com/nfroidure/schema2dts","commit_stats":{"total_commits":60,"total_committers":3,"mean_commits":20.0,"dds":0.06666666666666665,"last_synced_commit":"3ae34a6ee7e7ec1db9538c5cd8325ad2ecb53d14"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nfroidure%2Fschema2dts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nfroidure%2Fschema2dts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nfroidure%2Fschema2dts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nfroidure%2Fschema2dts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nfroidure","download_url":"https://codeload.github.com/nfroidure/schema2dts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219857296,"owners_count":16556070,"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":["hacktoberfest","jsonschema","openapi3"],"created_at":"2024-09-24T20:53:52.497Z","updated_at":"2025-10-29T21:30:43.800Z","avatar_url":"https://github.com/nfroidure.png","language":"TypeScript","funding_links":["https://github.com/sponsors/nfroidure"],"categories":["TypeScript"],"sub_categories":[],"readme":"[//]: # ( )\n[//]: # (This file is automatically generated by a `metapak`)\n[//]: # (module. Do not change it  except between the)\n[//]: # (`content:start/end` flags, your changes would)\n[//]: # (be overridden.)\n[//]: # ( )\n# schema2dts\n\u003e A very simple JSONSchema7/OpenAPI3 to TypeScript types definitions generator\n\n\n\n[//]: # (::contents:start)\n\nIt is intended to support JSONSchema7/OpenAPI3 but may work for some if not most\nJSONSchema versions.\n\nThis module assumes your JSONSchema / OpenAPI3 documents are valid. It also\ndoesn't support external references at the moment (and probably for ever) and\nexpect a single object whose definitions are all relative to the root object.\n\nIt is also meant to be a building block for higher level generators.\n\n## Usage\n\n```ts\nimport { readFile, writeFile } from 'node:fs/promises';\nimport {\n  generateJSONSchemaTypes,\n  generateOpenAPITypes,\n  toSource,\n} from 'schema2dts';\n\n// Open API\nconst openAPISchema = JSON.parse(readFileSync('openapi.json').toString());\n\nawait writeFile(\n  'API.d.ts',\n  toSource(await generateOpenAPITypes(openAPISchema)),\n);\n\n// JSON Schema\nconst jsonSchema = JSON.parse(readFileSync('schema.json').toString());\n\nawait writeFile(\n  'API.d.ts',\n  toSource(await generateJSONSchemaTypes(jsonSchema)),\n);\n```\n\nIf you find some cases with unexpected results, please add the fixtures to this\nrepository in a pull request and describe the problem you encounter.\n\n## Options\n\nYou can change the API main namespace in order to be able to use several\ngenerated types in the same repository. Just provide its namespace a the second\nargument to `generateOpenAPITypes`.\n\nThe third argument is for options:\n\n- you can generate the unused schemas (especially useful when in development\n  mode) to be able to use them in your code despite the fact they ain't used in\n  you API at that moment. Just set `generateUnusedSchemas` to `true`.\n- you can also filter the statuses you wish to generate by setting\n  `filterStatuses` to `[200, 201, 202, 300]` for example so that the 500 errors\n  responses ain't taken in count.\n\n## Known issues\n\nThere is some differences between the JSONSchema `anyOf`, `allOf` and `oneOf`\nkeywords (learn\n[more here on combining schemas](https://json-schema.org/understanding-json-schema/reference/combining.html)).\n\nThe current way to handle this in this library is to:\n\n- convert `oneOf` to an\n  [union type](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#union-types)\n  which is valid\n- convert `anyOf` to an union type too which is not really what it means in JSON\n  Schema\n- convert `allOf` to an\n  [intersection type](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#intersection-types)\n  which is completly wrong and will work only with JSON Schemas meant to be used\n  that way. By example, combining an existing object schema with another object\n  to make some properties required will only work if you set its type to\n  `object` in both schemas explicitly:\n\n```json\n{\n  \"allOf\": [\n    {\n      \"$ref\": \"#/definitions/User\"\n    },\n    { \"type\": \"object\", \"required\": [\"id\"] }\n  ]\n}\n```\n\nCurrently, the `if`/`then` keywords of JSONSchema do not work. You should be\nable to replace most of its use per a `oneOf` form.\n\n[//]: # (::contents:end)\n\n# API\n## Functions\n\n\u003cdl\u003e\n\u003cdt\u003e\u003ca href=\"#generateOpenAPITypes\"\u003egenerateOpenAPITypes(schema, options)\u003c/a\u003e ⇒ \u003ccode\u003eTypeScript.NodeArray\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eCreate the TypeScript types declarations from an Open API document\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#generateJSONSchemaTypes\"\u003egenerateJSONSchemaTypes(schema, options)\u003c/a\u003e ⇒ \u003ccode\u003eTypeScript.NodeArray\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eCreate the TypeScript types declarations from a JSONSchema document\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#toSource\"\u003etoSource(nodes)\u003c/a\u003e ⇒\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eReturns source from a list of TypeScript statements\u003c/p\u003e\n\u003c/dd\u003e\n\u003c/dl\u003e\n\n\u003ca name=\"generateOpenAPITypes\"\u003e\u003c/a\u003e\n\n## generateOpenAPITypes(schema, options) ⇒ \u003ccode\u003eTypeScript.NodeArray\u003c/code\u003e\nCreate the TypeScript types declarations from an Open API document\n\n**Kind**: global function  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| schema | \u003ccode\u003eJSONSchema.Document\u003c/code\u003e |  |\n| options | \u003ccode\u003eObject\u003c/code\u003e |  |\n| options.baseName | \u003ccode\u003estring\u003c/code\u003e |  |\n| options.filterStatuses | \u003ccode\u003eArray.\u0026lt;number\u0026gt;\u003c/code\u003e |  |\n| options.generateUnusedSchemas | \u003ccode\u003eboolean\u003c/code\u003e |  |\n| options.camelizeInputs | \u003ccode\u003eboolean\u003c/code\u003e |  |\n| options.brandedTypes | \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e | Brand types by names |\n| options.brandedFormats | \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e | Brand formats by names |\n| options.typedFormats | \u003ccode\u003eObject\u003c/code\u003e | Substitute string format by a type |\n| options.generateRealEnums | \u003ccode\u003eboolean\u003c/code\u003e |  |\n| options.tuplesFromFixedArraysLengthLimit | \u003ccode\u003enumber\u003c/code\u003e |  |\n| options.exportNamespaces | \u003ccode\u003eboolean\u003c/code\u003e |  |\n| options.requireCleanAPI | \u003ccode\u003eboolean\u003c/code\u003e |  |\n\n\u003ca name=\"generateJSONSchemaTypes\"\u003e\u003c/a\u003e\n\n## generateJSONSchemaTypes(schema, options) ⇒ \u003ccode\u003eTypeScript.NodeArray\u003c/code\u003e\nCreate the TypeScript types declarations from a JSONSchema document\n\n**Kind**: global function  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| schema | \u003ccode\u003eJSONSchema.Document\u003c/code\u003e |  |\n| options | \u003ccode\u003eObject\u003c/code\u003e |  |\n| options.baseName | \u003ccode\u003estring\u003c/code\u003e |  |\n| options.brandedTypes | \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e | Brand types by names |\n| options.brandedFormats | \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e | Brand formats by names |\n| options.typedFormats | \u003ccode\u003eObject\u003c/code\u003e | Substitute string format by a type |\n| options.generateRealEnums | \u003ccode\u003eboolean\u003c/code\u003e |  |\n| options.tuplesFromFixedArraysLengthLimit | \u003ccode\u003enumber\u003c/code\u003e |  |\n| options.exportNamespaces | \u003ccode\u003eboolean\u003c/code\u003e |  |\n\n\u003ca name=\"toSource\"\u003e\u003c/a\u003e\n\n## toSource(nodes) ⇒\nReturns source from a list of TypeScript statements\n\n**Kind**: global function  \n**Returns**: string  \n\n| Param | Type |\n| --- | --- |\n| nodes | \u003ccode\u003eTypedPropertyDescriptor.NodeArray\u003c/code\u003e | \n\n\n# Authors\n- [Nicolas Froidure](https://insertafter.com/en/index.html)\n\n# License\n[ISC](https://github.com/nfroidure/schema2dts/blob/main/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnfroidure%2Fschema2dts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnfroidure%2Fschema2dts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnfroidure%2Fschema2dts/lists"}