Skip to content

Commit

Permalink
add orval
Browse files Browse the repository at this point in the history
  • Loading branch information
ddecrulle committed May 26, 2024
1 parent e7bdd0a commit dd99af5
Show file tree
Hide file tree
Showing 8 changed files with 2,107 additions and 21 deletions.
73 changes: 73 additions & 0 deletions openApi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"openapi": "3.0.0",
"info": { "version": "1.0.6", "title": "My API" },
"components": {
"schemas": {
"Todo": {
"type": "object",
"properties": {
"id": { "type": "string", "example": "123" },
"text": { "type": "string", "example": "Clean my room" },
"isDone": { "type": "boolean", "example": false }
},
"required": ["id", "text", "isDone"]
}
},
"parameters": {}
},
"paths": {
"/todo/{id}": {
"put": {
"parameters": [
{
"schema": { "type": "string", "minLength": 1, "example": "1212121" },
"required": true,
"name": "id",
"in": "path"
},
{
"schema": { "type": "string", "minLength": 1, "example": "Clean my room" },
"required": true,
"name": "text",
"in": "query"
},
{
"schema": { "type": "boolean", "example": false },
"required": true,
"name": "isDone",
"in": "query"
}
],
"responses": { "200": { "description": "Create or update a todo item" } }
},
"delete": {
"parameters": [
{
"schema": { "type": "string", "minLength": 1, "example": "1212121" },
"required": true,
"name": "id",
"in": "path"
}
],
"responses": { "200": { "description": "Deleted a todo item" } }
}
},
"/todos": {
"get": {
"responses": {
"200": {
"description": "Get all user's todo",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": { "$ref": "#/components/schemas/Todo" }
}
}
}
}
}
}
}
}
}
22 changes: 22 additions & 0 deletions orval.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defineConfig } from "orval";

export default defineConfig({
openapi: {
input: "./openApi.json",
output: {
mode: "tags",
target: "src/api/",
schemas: "src/model/api",
client: "react-query",
override: {
mutator: {
path: "./src/api/axiosInstance.ts",
name: "axiosInstance"
}
}
},
hooks: {
afterAllFilesWrite: "yarn format"
}
}
});
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"format:check": "npm run _format -- --list-different",
"postinstall": "copy-dsfr-to-public",
"predev": "only-include-used-icons",
"prebuild": "only-include-used-icons"
"prebuild": "only-include-used-icons",
"openapi": "orval --config ./orval.config.ts"
},
"dependencies": {
"@codegouvfr/react-dsfr": "^1.9.11",
Expand Down Expand Up @@ -48,6 +49,7 @@
"eslint-plugin-react-refresh": "^0.4.6",
"husky": "^4.3.8",
"lint-staged": "^15.2.2",
"orval": "^6.29.1",
"prettier": "^3.2.5",
"typescript": "^5.4.5",
"vite": "^5.2.0"
Expand Down
167 changes: 167 additions & 0 deletions src/api/default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/**
* Generated by orval v6.29.1 🍺
* Do not edit manually.
* My API
* OpenAPI spec version: 1.0.6
*/
import { useMutation, useQuery } from "@tanstack/react-query";
import type {
MutationFunction,
QueryFunction,
QueryKey,
UseMutationOptions,
UseMutationResult,
UseQueryOptions,
UseQueryResult
} from "@tanstack/react-query";
import type { PutTodoIdParams, Todo } from "../model/api";
import { axiosInstance } from "./axiosInstance";

type SecondParameter<T extends (...args: any) => any> = Parameters<T>[1];

export const putTodoId = (
id: string,
params: PutTodoIdParams,
options?: SecondParameter<typeof axiosInstance>
) => {
return axiosInstance<void>({ url: `/todo/${id}`, method: "PUT", params }, options);
};

export const getPutTodoIdMutationOptions = <TError = unknown, TContext = unknown>(options?: {
mutation?: UseMutationOptions<
Awaited<ReturnType<typeof putTodoId>>,
TError,
{ id: string; params: PutTodoIdParams },
TContext
>;
request?: SecondParameter<typeof axiosInstance>;
}): UseMutationOptions<
Awaited<ReturnType<typeof putTodoId>>,
TError,
{ id: string; params: PutTodoIdParams },
TContext
> => {
const { mutation: mutationOptions, request: requestOptions } = options ?? {};

const mutationFn: MutationFunction<
Awaited<ReturnType<typeof putTodoId>>,
{ id: string; params: PutTodoIdParams }
> = props => {
const { id, params } = props ?? {};

return putTodoId(id, params, requestOptions);
};

return { mutationFn, ...mutationOptions };
};

export type PutTodoIdMutationResult = NonNullable<Awaited<ReturnType<typeof putTodoId>>>;

export type PutTodoIdMutationError = unknown;

export const usePutTodoId = <TError = unknown, TContext = unknown>(options?: {
mutation?: UseMutationOptions<
Awaited<ReturnType<typeof putTodoId>>,
TError,
{ id: string; params: PutTodoIdParams },
TContext
>;
request?: SecondParameter<typeof axiosInstance>;
}): UseMutationResult<
Awaited<ReturnType<typeof putTodoId>>,
TError,
{ id: string; params: PutTodoIdParams },
TContext
> => {
const mutationOptions = getPutTodoIdMutationOptions(options);

return useMutation(mutationOptions);
};
export const deleteTodoId = (id: string, options?: SecondParameter<typeof axiosInstance>) => {
return axiosInstance<void>({ url: `/todo/${id}`, method: "DELETE" }, options);
};

export const getDeleteTodoIdMutationOptions = <TError = unknown, TContext = unknown>(options?: {
mutation?: UseMutationOptions<
Awaited<ReturnType<typeof deleteTodoId>>,
TError,
{ id: string },
TContext
>;
request?: SecondParameter<typeof axiosInstance>;
}): UseMutationOptions<Awaited<ReturnType<typeof deleteTodoId>>, TError, { id: string }, TContext> => {
const { mutation: mutationOptions, request: requestOptions } = options ?? {};

const mutationFn: MutationFunction<
Awaited<ReturnType<typeof deleteTodoId>>,
{ id: string }
> = props => {
const { id } = props ?? {};

return deleteTodoId(id, requestOptions);
};

return { mutationFn, ...mutationOptions };
};

export type DeleteTodoIdMutationResult = NonNullable<Awaited<ReturnType<typeof deleteTodoId>>>;

export type DeleteTodoIdMutationError = unknown;

export const useDeleteTodoId = <TError = unknown, TContext = unknown>(options?: {
mutation?: UseMutationOptions<
Awaited<ReturnType<typeof deleteTodoId>>,
TError,
{ id: string },
TContext
>;
request?: SecondParameter<typeof axiosInstance>;
}): UseMutationResult<Awaited<ReturnType<typeof deleteTodoId>>, TError, { id: string }, TContext> => {
const mutationOptions = getDeleteTodoIdMutationOptions(options);

return useMutation(mutationOptions);
};
export const getTodos = (options?: SecondParameter<typeof axiosInstance>, signal?: AbortSignal) => {
return axiosInstance<Todo[]>({ url: `/todos`, method: "GET", signal }, options);
};

export const getGetTodosQueryKey = () => {
return [`/todos`] as const;
};

export const getGetTodosQueryOptions = <
TData = Awaited<ReturnType<typeof getTodos>>,
TError = unknown
>(options?: {
query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof getTodos>>, TError, TData>>;
request?: SecondParameter<typeof axiosInstance>;
}) => {
const { query: queryOptions, request: requestOptions } = options ?? {};

const queryKey = queryOptions?.queryKey ?? getGetTodosQueryKey();

const queryFn: QueryFunction<Awaited<ReturnType<typeof getTodos>>> = ({ signal }) =>
getTodos(requestOptions, signal);

return { queryKey, queryFn, ...queryOptions } as UseQueryOptions<
Awaited<ReturnType<typeof getTodos>>,
TError,
TData
> & { queryKey: QueryKey };
};

export type GetTodosQueryResult = NonNullable<Awaited<ReturnType<typeof getTodos>>>;
export type GetTodosQueryError = unknown;

export const useGetTodos = <TData = Awaited<ReturnType<typeof getTodos>>, TError = unknown>(options?: {
query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof getTodos>>, TError, TData>>;
request?: SecondParameter<typeof axiosInstance>;
}): UseQueryResult<TData, TError> & { queryKey: QueryKey } => {
const queryOptions = getGetTodosQueryOptions(options);

const query = useQuery(queryOptions) as UseQueryResult<TData, TError> & { queryKey: QueryKey };

query.queryKey = queryOptions.queryKey;

return query;
};
9 changes: 9 additions & 0 deletions src/model/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Generated by orval v6.29.1 🍺
* Do not edit manually.
* My API
* OpenAPI spec version: 1.0.6
*/

export * from "./putTodoIdParams";
export * from "./todo";
11 changes: 11 additions & 0 deletions src/model/api/putTodoIdParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Generated by orval v6.29.1 🍺
* Do not edit manually.
* My API
* OpenAPI spec version: 1.0.6
*/

export type PutTodoIdParams = {
text: string;
isDone: boolean;
};
12 changes: 12 additions & 0 deletions src/model/api/todo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Generated by orval v6.29.1 🍺
* Do not edit manually.
* My API
* OpenAPI spec version: 1.0.6
*/

export interface Todo {
id: string;
isDone: boolean;
text: string;
}
Loading

0 comments on commit dd99af5

Please sign in to comment.