{"id":16822220,"url":"https://github.com/strml/json-to-flow","last_synced_at":"2025-09-22T02:33:18.885Z","repository":{"id":49597747,"uuid":"47836380","full_name":"STRML/json-to-flow","owner":"STRML","description":"Convert model schemata into Flow types (.js.flow)","archived":false,"fork":false,"pushed_at":"2021-06-12T16:19:45.000Z","size":157,"stargazers_count":64,"open_issues_count":3,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-01-10T04:55:04.265Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/STRML.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}},"created_at":"2015-12-11T16:05:05.000Z","updated_at":"2022-02-23T13:30:19.000Z","dependencies_parsed_at":"2022-09-19T01:31:42.760Z","dependency_job_id":null,"html_url":"https://github.com/STRML/json-to-flow","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/STRML%2Fjson-to-flow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/STRML%2Fjson-to-flow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/STRML%2Fjson-to-flow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/STRML%2Fjson-to-flow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/STRML","download_url":"https://codeload.github.com/STRML/json-to-flow/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233819586,"owners_count":18735302,"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":[],"created_at":"2024-10-13T11:02:38.519Z","updated_at":"2025-09-22T02:33:13.630Z","avatar_url":"https://github.com/STRML.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# json-to-flow\n\nConvert [swagger](http://swagger.io) model schemata into\nFlow [declaration files](http://flowtype.org/blog/2015/12/01/Version-0.19.0.html#declaration-files) (.js.flow).\n\n### Usage\n\nSee [the template](template.ejs) for more details.\n\nYou can use your own custom template via the `templatePath` property, or your own template function\nvia the `templateFn` property. See the [Options](#options) below.\n\n```js\nconst jsonToFlow = require('json-to-flow');\n\n// You can generate this yourself from tooling,\n// or use the `definitions` property on a Swagger spec.\n// Note that this has changed to include the full definition object\n// as of 1.0.0.\nconst schema = {\n  User: {\n    properties: {\n      // primitives\n      id: {type: 'number'},\n      fullname: {type: 'string'},\n      verified: {type: 'boolean'},\n      // builtins\n      created: {type: 'Date'},\n      matcher: {type: 'RegExp'},\n\n      // custom types\n      childObj: {type: 'CustomType'},\n\n      // arrays (uses swagger array spec)\n      array: {\n        type: 'array',\n        items: {\n          type: 'string' // could be a custom type or builtin too\n        }\n      },\n      arrayModels: {\n        type: 'array',\n        items: {\n          // will translate this into type: 'Pet'\n          $ref:  '#/definitions/Pet'\n        }\n      },\n\n      // Not part of swagger, but illustrating you can put anything in\n      literal: {\n        type: 'Promise\u003cArray\u003cFoo\u003e\u003e'\n      }\n    },\n    required: [\n      \"id\",\n      \"fullname\"\n    ],\n    // If true, will export an inexact object (`{...}`)\n    additionalProperties: false\n  }\n};\njsonToFlow(schema, {\n  templateData: {\n    modelSuperClass: 'Model',\n    modelSuperClassPath: 'models/_model'\n  },\n  targetPath: path.join(__dirname, 'models'),\n  // templatePath: string, // Pass an optional abs ejs file path, or\n  // templateFn: (data: {modelName: string, modelSchema: string,\n  //                   modelSuperClass: string, ...options}) =\u003e string\n}).then((results) =\u003e {\n  console.log(results);\n})\n.catch(console.error);\n```\n\nYou can also use the CLI:\n\n```\nyarn json-to-flow --help\n\nOptions:\n  --help               Show help                                       [boolean]\n  --version            Show version number                             [boolean]\n  --superClass         The name of the class your generated models should\n                       extend.                       [string] [default: \"Model\"]\n  --superClassPath     The import path for the generated model superclass.\n                                             [string] [default: \"models/_model\"]\n  --templatePath       Optional template file override.                 [string]\n  --targetPath         The target path for generated files.  [string] [required]\n  --templateExtension  The output file extension. [string] [default: \".js.flow\"]\n  --inputPath          The input Swagger/OpenAPI JSON file path.\n                                                             [string] [required]\n\n$ yarn json-to-flow --inputPath swagger.json --targetPath types/auto-generated/\n```\n\n#### Options\n\n```js\n\nexport type FullSwaggerSchema = {\n  [key: string]: SwaggerModelSchema,\n};\nexport type SwaggerModelSchema = {\n  properties: {[key: string]: Field},\n  required?: string[],\n  additionalProperties?: boolean,\n};\nexport type ModelSchema = {\n  [key: string]: FieldOutput;\n};\nexport type AllModelSchemata = {\n  [modelName: string]: ModelSchema\n};\n// Note you can add your own fields to this, if your template knows to expect them.\nexport type TemplateData = {\n  // In the default template, this is a map of types to add to the top of the file\n  additionalTypes: {[key: string]: Field},\n  // In default template, this is what the exported class extends\n  modelSuperClass: string,\n  // This is the file location of that extended class\n  modelSuperClassPath: string,\n  modelName: string, // automatically filled in\n  modelSchema: ModelSchema, // automatically filled in\n};\nexport type Field = {type: string, $ref?: string, format?: string, items?: Field};\nexport type FieldOutput = {type: string, $ref?: string, format?: string, required: boolean};\n\nexport type Options = {\n  // The path to the default template. You can override this. The entirety of the options\n  // object is passed to the template so you can add anything you like.\n  templatePath: string = path.join(__dirname, 'template.ejs'),\n\n  //\n  // Variables used in default template.\n  // Note that `modelName` and `modelSchema` is automatically merged into this on every iteration.\n  //\n  templateData: $Shape\u003cTemplateData\u003e = {modelSuperClass: 'Model', modelSuperClassPath: 'models/_model'},\n\n  // If present, defines where the output goes. You can also pass a function.\n  // If not present, will call back with compiled template.\n  targetPath?: string | (modelName: string) =\u003e string,\n\n  // The default target file extension. Note the leading `.`, intentional so you can add suffixes etc.\n  templateExtension: string = '.js.flow',\n\n  // By default, this translator takes in Swagger spec data and translates it to JS as closely\n  // as is reasonable. You may not like how it does or may want to add your own hooks - do it here.\n  // You can access the default translator at require('json-to-flow').defaults.translateField.\n  translateField: (Field, Options, SwaggerModelSchema, string) =\u003e FieldOutput = defaults.fieldTranslator,\n\n  // Optional hook for transforming data right before it is passed to templateFn.\n  preTemplateFn?: ?(TemplateData) =\u003e TemplateData,\n\n  // This is populated at runtime with an ejs.compile() call. Replace this if you want to use another\n  // template engine.\n  templateFn: (TemplateData) =\u003e string,\n};\n```\n\n#### Example Output\n\n```js\nimport {Model} from 'models/_model';\n\nexport class UserModel extends Model {\n  id: number;\n  fullname: string;\n  verified: boolean;\n  created: Date;\n  matcher: RegExp;\n  childObj: CustomType;\n  optionalType: ?string,\n  array: Array\u003cstring\u003e;\n  arrayModels: Array\u003cPet\u003e;\n  literal: Promise\u003cArray\u003cFoo\u003e\u003e;\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrml%2Fjson-to-flow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstrml%2Fjson-to-flow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrml%2Fjson-to-flow/lists"}