Skip to content

Commit

Permalink
feat: add put verb (#41)
Browse files Browse the repository at this point in the history
Co-authored-by: Maxime Larichelliere <Galabar64@users.noreply.github.com>
  • Loading branch information
Galabar64 and Galabar64 committed May 22, 2023
1 parent 31bd754 commit 771da89
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ The following code snippets are only provided as example. It's best to start a n
`Todo.controller.ts`

```ts
import { Controller, Delete, Get, Post, Status } from '@fluxapi/common';
import { Controller, Delete, Get, Post, Put, Status } from '@fluxapi/common';
import {
CreateTodo,
ListTodoQuery,
Expand All @@ -111,7 +111,7 @@ export class TodoController {
// implementation
}

@Post('/:id')
@Put('/:id')
async update(id: number, body: UpdateTodo): Promise<TodoResponse> {
// implementation
}
Expand Down
7 changes: 7 additions & 0 deletions packages/common/src/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ export function Post(url: string = ''): MethodDecorator {
};
}

export function Put(url: string = ''): MethodDecorator {
return function (target: Object, functionName: string | symbol) {
addProperty(target, functionName, 'url', url);
addProperty(target, functionName, 'method', 'PUT');
};
}

export function Status(code: number): MethodDecorator {
return function (target: Object, functionName: string | symbol) {
addProperty(target, functionName, 'statusCode', code);
Expand Down
4 changes: 2 additions & 2 deletions playground/integration-tests/src/crud/crud.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Delete, Get, Post, Status } from 'fastify-flux';
import { Controller, Delete, Get, Post, Put, Status } from 'fastify-flux';
import { CreateTodo, ListTodoQuery, TodoResponse, UpdateTodo } from './crud.schema';
import { HttpException } from '~/helper/exceptions';

Expand Down Expand Up @@ -32,7 +32,7 @@ export class CrudController {
return found;
}

@Post('/:id')
@Put('/:id')
async update(id: number, body: UpdateTodo): Promise<TodoResponse> {
const found = todos.find((x) => x.id === id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ exports[`schema 1`] = `
"todos",
],
},
"post": {
"put": {
"operationId": "update",
"parameters": [
{
Expand Down
4 changes: 2 additions & 2 deletions playground/template-prisma/src/controllers/Todo.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Delete, Get, Post, Status } from 'fastify-flux';
import { Controller, Delete, Get, Post, Put, Status } from 'fastify-flux';
import { CreateTodo, ListTodoQuery, TodoResponse, UpdateTodo } from './Todo.schema';
import { HttpException } from '~/helper/exceptions';
import { PrismaClient } from '@prisma/client';
Expand Down Expand Up @@ -32,7 +32,7 @@ export class TodoController {
return found;
}

@Post('/:id')
@Put('/:id')
async update(id: number, body: UpdateTodo): Promise<TodoResponse> {
const found = await prisma.todos.findUnique({ where: { id } });

Expand Down
12 changes: 2 additions & 10 deletions playground/template-prisma/tests-e2e/api/GeneratedApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,13 @@ export class HttpClient<SecurityDataType = unknown> {
const requestParams = params;
const responseFormat = (format && this.format) || void 0;

if (!type) {
type = ContentType.Json;
}

if (!body) {
body = {};
}

try {
const result: any = await this.instance.request({
...requestParams,
headers: {
...(type && type !== ContentType.FormData ? { 'Content-Type': type } : {}),
...(requestParams.headers || {}),
} as any,
params: query,
responseType: responseFormat,
data: body,
Expand Down Expand Up @@ -220,7 +212,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
*
* @tags todos
* @name Update
* @request POST:/todos/{id}
* @request PUT:/todos/{id}
*/
update: (
id: number,
Expand All @@ -245,7 +237,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
any
>({
path: `/todos/${id}`,
method: 'POST',
method: 'PUT',
body: data,
type: ContentType.Json,
format: 'json',
Expand Down
6 changes: 6 additions & 0 deletions playground/template-prisma/tests-e2e/todo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ test('create todo', async () => {
await client.todos.create({ text: 'foobar', priority: 1 });
});

test('update todo', async () => {
const result = await client.todos.create({ text: 'foobar', priority: 1 });
await client.todos.get(result.id);
await client.todos.update(result.id, { text: 'barfoo', priority: 2 });
});

test('get todo', async () => {
const result = await client.todos.create({ text: 'foobar', priority: 1 });
await client.todos.get(result.id);
Expand Down

0 comments on commit 771da89

Please sign in to comment.