Skip to content

Commit

Permalink
Merge pull request #288 from hey-api/docs/json-schemas
Browse files Browse the repository at this point in the history
fix(config): rename exportSchemas to schemas
  • Loading branch information
mrlubos authored Apr 8, 2024
2 parents cd8970f + 443231b commit 25ba56f
Show file tree
Hide file tree
Showing 21 changed files with 77 additions and 44 deletions.
6 changes: 6 additions & 0 deletions .changeset/hot-turkeys-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@hey-api/openapi-ts": patch
"openapi-ts-docs": patch
---

fix(config): rename exportSchemas to schemas
3 changes: 3 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ features:
- icon: 🦴
title: Data Fetching
details: Typesafe data with our REST clients. Fetch, axios, angular, node, and xhr are available.
- icon: "{✓}"
title: JSON Schemas
details: Export OpenAPI schemas as JavaScript objects. JSON Schema 2020-12 supported.
---

### Migrating from OpenAPI Typescript Codegen?
Expand Down
24 changes: 24 additions & 0 deletions docs/openapi-ts/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,30 @@ export default {
}
```

## JSON Schemas

By default, `openapi-ts` exports schemas from your OpenAPI specification as plain JavaScript objects. A great use case for schemas is client-side form input validation.

```ts
import { $Schema } from 'src/client'

const maxInputLength = $Schema.properties.text.maxLength

if (userInput.length > maxInputLength) {
throw new Error(`String length cannot exceed ${maxInputLength} characters!`)
}
```

If you're using OpenAPI v3.1, your schemas are JSON Schema compliant and can be used with any other tools supporting JSON Schema. However, if you don't need schemas at all, you can disable them with

```js{4}
export default {
input: 'path/to/openapi.json',
output: 'src/client',
schemas: false,
}
```

## Config API

You can view the complete list of options in the [UserConfig](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/types/config.ts) interface.
2 changes: 1 addition & 1 deletion docs/openapi-ts/migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ This way, you can gradually switch over to the new syntax as you update parts of
### Changes
- `exportSchemas` is `true` by default (set `exportSchemas` to `false` to preserve the old behavior)
- `exportSchemas` is `true` by default (set `exportSchemas` to `false` to preserve the old behavior, in v0.36.0 this has been renamed to `schemas`)
- `client` is optional (old behavior preserved, but can be most likely removed)
### Removed
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ const params = program
.option('--enums <value>', 'Export enum definitions (javascript, typescript)')
.option('--exportCore [value]', 'Write core files to disk')
.option('--exportModels [value]', 'Write models to disk')
.option('--exportSchemas [value]', 'Write schemas to disk')
.option('--exportServices [value]', 'Write services to disk')
.option('--format [value]', 'Process output folder with formatter?')
.option('--lint [value]', 'Process output folder with linter?')
.option('--name <value>', 'Custom client class name')
.option('--operationId [value]', 'Use operationd ID?')
.option('--postfixServices <value>', 'Service name postfix')
.option('--request <value>', 'Path to custom request file')
.option('--schemas [value]', 'Write schemas to disk')
.option('--serviceResponse [value]', 'Define shape of returned value from service calls')
.option('--useDateType [value]', 'Output Date instead of string for the format "date-time" in the models')
.option('--useOptions [value]', 'Use options instead of arguments')
Expand Down Expand Up @@ -64,11 +64,11 @@ async function start() {
...processParams(params, [
'exportCore',
'exportModels',
'exportSchemas',
'exportServices',
'format',
'lint',
'operationId',
'schemas',
'useDateType',
'useOptions',
'write',
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) =>
experimental = false,
exportCore = true,
exportModels = true,
exportSchemas = true,
exportServices = true,
format = true,
input,
Expand All @@ -104,6 +103,7 @@ const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) =>
operationId = true,
postfixServices = 'Service',
request,
schemas = true,
serviceResponse = 'body',
useDateType = false,
useOptions = true,
Expand Down Expand Up @@ -149,7 +149,6 @@ const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) =>
experimental,
exportCore,
exportModels,
exportSchemas,
exportServices,
format,
input,
Expand All @@ -159,6 +158,7 @@ const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) =>
output,
postfixServices,
request,
schemas,
serviceResponse,
useDateType,
useOptions,
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/openApi/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ describe('parse', () => {
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: true,
input: '',
lint: false,
operationId: true,
output: '',
postfixServices: '',
schemas: true,
serviceResponse: 'body',
useDateType: false,
useOptions: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ describe('getServices', () => {
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: false,
input: '',
lint: false,
operationId: false,
output: '',
postfixServices: 'Service',
schemas: true,
serviceResponse: 'body',
useDateType: false,
useOptions: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ describe('getServices', () => {
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: false,
input: '',
lint: false,
operationId: true,
output: '',
postfixServices: 'Service',
schemas: true,
serviceResponse: 'body',
useDateType: false,
useOptions: true,
Expand Down
10 changes: 5 additions & 5 deletions packages/openapi-ts/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ export interface UserConfig {
* @default true
*/
exportModels?: boolean | string;
/**
* Generate schemas?
* @default true
*/
exportSchemas?: boolean;
/**
* Generate services?
* @default true
Expand Down Expand Up @@ -79,6 +74,11 @@ export interface UserConfig {
* Path to custom request file
*/
request?: string;
/**
* Export JSON schemas?
* @default true
*/
schemas?: boolean;
/**
* Define shape of returned value from service calls
* @default 'body'
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ describe('registerHandlebarHelpers', () => {
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: true,
input: '',
lint: false,
operationId: true,
output: '',
postfixServices: '',
schemas: true,
serviceResponse: 'body',
useDateType: false,
useOptions: false,
Expand Down Expand Up @@ -49,14 +49,14 @@ describe('registerHandlebarTemplates', () => {
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: true,
input: '',
lint: false,
operationId: true,
output: '',
postfixServices: '',
schemas: true,
serviceResponse: 'body',
useDateType: false,
useOptions: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ describe('writeClientClass', () => {
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: false,
input: '',
Expand All @@ -38,6 +37,7 @@ describe('writeClientClass', () => {
operationId: true,
output: '',
postfixServices: '',
schemas: true,
serviceResponse: 'body',
useDateType: false,
useOptions: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ describe('writeClient', () => {
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: true,
input: '',
lint: false,
operationId: true,
output: './dist',
postfixServices: 'Service',
schemas: true,
serviceResponse: 'body',
useDateType: false,
useOptions: false,
Expand Down
6 changes: 3 additions & 3 deletions packages/openapi-ts/src/utils/write/__tests__/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe('writeClientCore', () => {
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: false,
input: '',
Expand All @@ -40,6 +39,7 @@ describe('writeClientCore', () => {
operationId: true,
output: '',
postfixServices: '',
schemas: true,
serviceResponse: 'body',
useDateType: false,
useOptions: true,
Expand Down Expand Up @@ -73,7 +73,6 @@ describe('writeClientCore', () => {
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: false,
input: '',
Expand All @@ -82,6 +81,7 @@ describe('writeClientCore', () => {
operationId: true,
output: '',
postfixServices: '',
schemas: true,
serviceResponse: 'body',
useDateType: false,
useOptions: true,
Expand Down Expand Up @@ -115,7 +115,6 @@ describe('writeClientCore', () => {
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: false,
input: '',
Expand All @@ -124,6 +123,7 @@ describe('writeClientCore', () => {
operationId: true,
output: '',
postfixServices: '',
schemas: true,
serviceResponse: 'body',
useDateType: false,
useOptions: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ describe('writeClientIndex', () => {
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: false,
input: '',
lint: false,
operationId: true,
output: '',
postfixServices: 'Service',
schemas: true,
serviceResponse: 'body',
useDateType: false,
useOptions: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ describe('writeClientModels', () => {
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: false,
input: '',
Expand All @@ -53,6 +52,7 @@ describe('writeClientModels', () => {
operationId: true,
output: '',
postfixServices: '',
schemas: true,
serviceResponse: 'body',
useDateType: false,
useOptions: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ describe('writeClientServices', () => {
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: false,
input: '',
lint: false,
operationId: true,
output: '',
postfixServices: 'Service',
schemas: true,
serviceResponse: 'body',
useDateType: false,
useOptions: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/utils/write/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const writeClient = async (
},
{
dir: '',
enabled: config.exportSchemas,
enabled: config.schemas,
fn: writeClientSchemas,
},
{
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/utils/write/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const writeClientIndex = async (client: Client, outputPath: string, confi
if (config.exportModels) {
file.add(compiler.export.all('./models'));
}
if (config.exportSchemas) {
if (config.schemas) {
file.add(compiler.export.all('./schemas'));
}
}
Expand Down
Loading

0 comments on commit 25ba56f

Please sign in to comment.