Skip to content

Commit

Permalink
import code
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasAribart committed Jul 14, 2020
0 parents commit 6ea5dc7
Show file tree
Hide file tree
Showing 36 changed files with 6,009 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
version: 2

defaults: &defaults
working_directory: ~/repo
docker:
- image: circleci/node:12.10.0

jobs:
test:
<<: *defaults
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
- v1-dependencies-
- run: yarn
- run:
name: Run tests
command: yarn test
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- persist_to_workspace:
root: ~/repo
paths: .

deploy:
<<: *defaults
steps:
- attach_workspace:
at: ~/repo
- run:
name: Set registry URL
command: npm set registry https://packagecloud.io/thomasaribart/json-typescript/npm/
- run:
name: Authenticate with registry
command: echo "//packagecloud.io/thomasaribart/json-typescript/npm/:_authToken=$NPM_TOKEN" > ~/repo/.npmrc
- run:
name: Publish package
command: npm publish

workflows:
version: 2
test-deploy:
jobs:
- test
- deploy:
requires:
- test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Thomas Aribart

Permission 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:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE 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.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# json-typescript
Infer types from your JSON schemas
6 changes: 6 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
],
};
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "json-schema-to-ts",
"version": "0.1.0",
"description": "Infer typescript types from your JSON schemas!",
"main": "index.ts",
"scripts": {
"test": "jest"
},
"devDependencies": {
"@babel/core": "^7.10.4",
"@babel/preset-env": "^7.10.4",
"@babel/preset-typescript": "^7.10.4",
"@types/jest": "^26.0.4",
"ajv": "^6.12.3",
"babel-jest": "^26.1.0",
"jest": "^26.1.0",
"typescript": "^3.9.6"
},
"author": "Thomas Aribart",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/ThomasAribart/json-schema-to-ts.git"
},
"keywords": [
"json",
"schema",
"typescript",
"type",
"ts"
],
"bugs": {
"url": "https://github.com/ThomasAribart/json-schema-to-ts/issues"
},
"homepage": "https://github.com/ThomasAribart/json-schema-to-ts#readme"
}
31 changes: 31 additions & 0 deletions src/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { FromWriteableSchema } from "./index";
import { Schema } from "./schema";
import { Head, Tail, Prepend, ConcatReversed, ShortenReversed } from "./utils";

export type FromArraySchema<S> = "type" extends keyof S
? S["type"] extends "array"
? "items" extends keyof S
? S["items"] extends Schema
? FromWriteableSchema<S["items"]>[]
: RecurseOnTupleSchema<
S["items"],
"additionalItems" extends keyof S ? S["additionalItems"] : true
>
: any[]
: never
: never;

type RecurseOnTupleSchema<S, A, R extends any[] = []> = {
stop: A extends false
? ShortenReversed<R>
: ShortenReversed<R> | ConcatReversed<R, [...any[]]>;
continue: S extends any[]
? Head<S> extends Schema
? RecurseOnTupleSchema<
Tail<S>,
A,
Prepend<FromWriteableSchema<Head<S>>, R>
>
: never
: never;
}[S extends [any, ...any[]] ? "continue" : "stop"];
7 changes: 7 additions & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { FromWriteableSchema } from "./index";

export type FromConstSchema<S> = "const" extends keyof S
? "type" extends keyof S
? S["const"] & FromWriteableSchema<Omit<S, "const" | "enum">>
: S["const"]
: never;
17 changes: 17 additions & 0 deletions src/enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { FromWriteableSchema } from "./index";
import { Head, Tail } from "./utils";

export type FromEnumSchema<S> = "enum" extends keyof S
? "type" extends keyof S
? RecurseOnEnumSchema<S["enum"], FromWriteableSchema<Omit<S, "enum">>>
: RecurseOnEnumSchema<S["enum"]>
: never;

type RecurseOnEnumSchema<S, T = any, R = never> = {
stop: R;
continue: S extends any[]
? Head<S> extends T
? RecurseOnEnumSchema<Tail<S>, T, R | Head<S>>
: RecurseOnEnumSchema<Tail<S>, T, R>
: never;
}[S extends [any, ...any[]] ? "continue" : "stop"];
46 changes: 46 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { FromObjectSchema } from "./object";
import { FromArraySchema } from "./array";
import { FromConstSchema } from "./const";
import { FromEnumSchema } from "./enum";
import { Writeable } from "./utils";

export type FromSchema<S> = FromReadonlySchema<S>;

type FromReadonlySchema<S> = FromWriteableSchema<Writeable<S>>;

export type FromWriteableSchema<S> = {
any: any;
never: never;
null: null;
boolean: boolean;
string: string;
number: number;
object: FromObjectSchema<S>;
array: FromArraySchema<S>;
const: FromConstSchema<S>;
enum: FromEnumSchema<S>;
}[InferSchemaType<S>];

type InferSchemaType<S> = S extends true | string
? "any"
: S extends false
? "never"
: "const" extends keyof S
? "const"
: "enum" extends keyof S
? "enum"
: "type" extends keyof S
? S["type"] extends "null"
? "null"
: S["type"] extends "boolean"
? "boolean"
: S["type"] extends "integer" | "number"
? "number"
: S["type"] extends "string"
? "string"
: S["type"] extends "object"
? "object"
: S["type"] extends "array"
? "array"
: never
: never;
29 changes: 29 additions & 0 deletions src/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { FromWriteableSchema } from "./index";
import { ObjectSchema, Schema } from "./schema";
import { Merge } from "./utils";

export type FromObjectSchema<S> = S extends ObjectSchema
? "properties" extends keyof S
? number extends keyof S["required"]
? Merge<
{
[key in Exclude<
keyof S["properties"],
S["required"][number]
>]?: S["properties"][key] extends Schema
? FromWriteableSchema<S["properties"][key]>
: never;
},
{
[key in S["required"][number]]: FromWriteableSchema<
S["properties"][key]
>;
}
>
: {
[key in keyof S["properties"]]?: S["properties"][key] extends Schema
? FromWriteableSchema<S["properties"][key]>
: never;
}
: object
: never;
101 changes: 101 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { Merge } from "./utils";

type CommonProps = {
title?: string;
description?: string;
default?: any;
deprecated?: boolean;
readOnly?: boolean;
writeOnly?: boolean;
examples?: any[];
};

type MakeSchema<SpecificProps = {}> = Merge<CommonProps, SpecificProps>;

export type NullSchema = MakeSchema<{
type: "null";
}>;

export type BooleanSchema = MakeSchema<{
type: "boolean";
}>;

export type StringSchema = MakeSchema<{
type: "string";
maxLength?: number;
minLength?: number;
pattern?: string;
}>;

export type IntegerSchema = MakeSchema<{
type: "integer";
multipleOf?: number;
maximum?: number;
exclusiveMaximum?: number;
minimum?: number;
exclusiveMinimum?: number;
}>;

export type NumberSchema = MakeSchema<{
type: "number";
multipleOf?: number;
maximum?: number;
exclusiveMaximum?: number;
minimum?: number;
exclusiveMinimum?: number;
}>;

export type ObjectSchema = MakeSchema<{
type: "object";
properties?: { [propertyName: string]: Schema };
required?: any[];
dependentRequired?: { [property: string]: string[] };
maxProperties?: number;
minProperties?: number;
}>;

export type ArraySchema = MakeSchema<{
type: "array";
items?: Schema | any[];
maxItems?: number;
minItems?: number;
uniqueItems?: boolean;
additionalItems?: boolean;
}>;

export type ConstSchema = MakeSchema<{
type?:
| "null"
| "boolean"
| "string"
| "integer"
| "number"
| "object"
| "array";
const: any;
}>;

export type EnumSchema = MakeSchema<{
type?:
| "null"
| "boolean"
| "string"
| "integer"
| "number"
| "object"
| "array";
enum: any[];
}>;

export type Schema =
| boolean
| string
| NullSchema
| BooleanSchema
| StringSchema
| IntegerSchema
| NumberSchema
| ObjectSchema
| ArraySchema
| ConstSchema
| EnumSchema;
Loading

0 comments on commit 6ea5dc7

Please sign in to comment.