Skip to content

Commit

Permalink
v0.2.0
Browse files Browse the repository at this point in the history
This change breaks the API by changing the previous logic
used for both input/output validation and the OpenAPI generation
so that only a simple default specification is generated from the newly
introfuced `input` and `output` logic, whereas the user can override
these with the `openApiSpec` object that can now be used on three
levels, similarly to the middlewares and error handlers.

This change also contains miscellaneous refactorings for the project
structure due to the increasing amount of utility functions etc.
  • Loading branch information
blomqma committed Jan 3, 2023
1 parent ddef007 commit ca08104
Show file tree
Hide file tree
Showing 49 changed files with 3,151 additions and 5,245 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
node_modules
coverage
dist
tsconfig.tsbuildinfo
.next
next-env.d.ts
build
.docusaurus
2 changes: 0 additions & 2 deletions .npmrc

This file was deleted.

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
All notable changes to this project will be documented in this file.
We follow the [Semantic Versioning 2.0.0](http://semver.org/) format.

### 0.2.0 - 2023-01-03

### Changed

- The public API is changed in a way that the OpenAPI spec generation is split out from the input/output validation. The validation is now done with `input` and `output` keywords within the `defineEndpoints` handler, that generates a minimal OpenAPI spec out of the contents of the input and output definitions and all other OpenAPI generation-related overrides are done inside the `openApiSpec` object. This makes the separation of the business logic and documentation clearer, while still auto-generating the definitions from the application logic.

### 0.1.2 - 2022-12-09

### Fixed
Expand Down
36 changes: 0 additions & 36 deletions apps/dev/.gitignore

This file was deleted.

4 changes: 2 additions & 2 deletions apps/dev/pages/api/[[...next-rest-framework]].ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { defineCatchAllHandler } from 'next-rest-framework/client';
import * as z from 'zod';
import { z } from 'zod';

export default defineCatchAllHandler({
GET: {
responses: [
output: [
{
status: 200,
contentType: 'text/plain',
Expand Down
130 changes: 65 additions & 65 deletions apps/dev/pages/api/todos.ts
Original file line number Diff line number Diff line change
@@ -1,108 +1,108 @@
import sampleData from 'sample-data.json';
import * as z from 'zod';
import * as yup from 'yup';
import { defineEndpoints } from 'next-rest-framework/client';
import { z } from 'zod';
import * as yup from 'yup';

export default defineEndpoints({
middleware: ({ params: { foo, bar, baz } }) => ({
foo: bar,
bar: baz,
baz: foo,
asd: 'asd'
baz: foo
}),
GET: {
middleware: () => ({
qux: 'qux'
}),
responses: [
output: [
{
status: 200,
contentType: 'text/html',
schema: z.object({
data: z.array(
z.object({
userId: z.number(),
id: z.number(),
title: z.string(),
completed: z.boolean()
})
)
foo: z.string(),
bar: z.string(),
baz: z.string(),
qux: z.string()
})
}
],
handler: async ({ res, params: { foo, bar, baz, qux } }) => {
console.log({ foo, bar, baz, qux });
res.setHeader('content-type', 'text/html');
res.status(200).json({ data: sampleData });
res.status(200).json({ foo, bar, baz, qux });
}
},
POST: {
tags: ['Todo'],
description: 'Create a new todo',
externalDocs: {
description: 'Find out more about Swagger',
url: 'http://swagger.io'
input: {
contentType: 'application/json',
schema: z.object({
foo: z.string(),
bar: z.number()
})
},
operationId: 'createTodo',
parameters: [
output: [
{
name: 'todo',
in: 'body',
description: 'The todo to create',
required: true,
deprecated: false,
allowEmptyValue: false
}
],
deprecated: false,
security: [
{
foo: ['bar']
}
],
servers: [
{
url: 'http://localhost:3000',
description: 'Development server',
variables: {}
status: 201,
contentType: 'application/json',
schema: z.object({
foo: z.string(),
bar: z.number(),
qux: z.array(
z.object({
qux: z.string()
})
)
})
}
],
requestBody: {
description: 'The todo to create',
middleware: () => ({
qux: 'qux'
}),
handler: async ({
req: {
body: { foo, bar }
},
res,
params: { qux }
}) => {
res.status(201).json({ foo, bar, qux: [{ qux }] });
}
},
PUT: {
input: {
contentType: 'application/json',
schema: yup.object({
foo: yup.string(),
bar: yup.number()
}),
example: {
foo: 'Buy milk',
bar: 1
},
examples: {},
encoding: {},
required: true
foo: yup.array(
yup.object({
bar: yup.string()
})
),
baz: yup.number()
})
},
responses: [
output: [
{
status: 201,
contentType: 'application/json',
schema: z.object({
data: z.array(
z.object({
userId: z.number(),
id: z.number(),
title: z.string(),
completed: z.boolean()
schema: yup.object({
foo: yup.array(
yup.object({
bar: yup.string()
})
)
),
bar: yup.number(),
qux: yup.string()
})
}
],
middleware: () => ({
qux: 'qux'
}),
handler: async ({ res }) => {
res.status(201).json({ data: sampleData });
handler: async ({
req: {
body: { foo }
},
res,
params: { qux }
}) => {
res.status(201).json({ foo, bar: 0, qux });
}
}
});
Loading

1 comment on commit ca08104

@vercel
Copy link

@vercel vercel bot commented on ca08104 Jan 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.