|
| 1 | +--- |
| 2 | +to: src/services/api/services/<%= h.inflection.transform(name, ['pluralize', 'underscore', 'dasherize']) %>.ts |
| 3 | +--- |
| 4 | +import { useCallback } from "react"; |
| 5 | +import useFetch from "../use-fetch"; |
| 6 | +import { API_URL } from "../config"; |
| 7 | +import wrapperFetchJsonResponse from "../wrapper-fetch-json-response"; |
| 8 | +import { InfinityPaginationType } from "../types/infinity-pagination"; |
| 9 | +import { RequestConfigType } from "./types/request-config"; |
| 10 | +import { <%= name %> as Entity } from "../types/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>"; |
| 11 | + |
| 12 | +export type Get<%= h.inflection.transform(name, ['pluralize']) %>Request = { |
| 13 | + page: number; |
| 14 | + limit: number; |
| 15 | +}; |
| 16 | + |
| 17 | +export type Get<%= h.inflection.transform(name, ['pluralize']) %>Response = InfinityPaginationType<Entity>; |
| 18 | + |
| 19 | +export function useGet<%= h.inflection.transform(name, ['pluralize']) %>Service() { |
| 20 | + const fetch = useFetch(); |
| 21 | + |
| 22 | + return useCallback( |
| 23 | + (data: Get<%= h.inflection.transform(name, ['pluralize']) %>Request, requestConfig?: RequestConfigType) => { |
| 24 | + const requestUrl = new URL(`${API_URL}/v1/<%= h.inflection.transform(name, ['pluralize', 'underscore', 'dasherize']) %>`); |
| 25 | + requestUrl.searchParams.append("page", data.page.toString()); |
| 26 | + requestUrl.searchParams.append("limit", data.limit.toString()); |
| 27 | + |
| 28 | + return fetch(requestUrl, { |
| 29 | + method: "GET", |
| 30 | + ...requestConfig, |
| 31 | + }).then(wrapperFetchJsonResponse<Get<%= h.inflection.transform(name, ['pluralize']) %>Response>); |
| 32 | + }, |
| 33 | + [fetch] |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +export type Get<%= name %>Request = { |
| 38 | + id: Entity["id"]; |
| 39 | +}; |
| 40 | + |
| 41 | +export type Get<%= name %>Response = Entity; |
| 42 | + |
| 43 | +export function useGet<%= name %>Service() { |
| 44 | + const fetch = useFetch(); |
| 45 | + |
| 46 | + return useCallback( |
| 47 | + (data: Get<%= name %>Request, requestConfig?: RequestConfigType) => { |
| 48 | + return fetch(`${API_URL}/v1/<%= h.inflection.transform(name, ['pluralize', 'underscore', 'dasherize']) %>/${data.id}`, { |
| 49 | + method: "GET", |
| 50 | + ...requestConfig, |
| 51 | + }).then(wrapperFetchJsonResponse<Get<%= name %>Response>); |
| 52 | + }, |
| 53 | + [fetch] |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +export type Create<%= name %>Request = Pick<Entity, "description">; |
| 58 | + |
| 59 | +export type Create<%= name %>Response = Entity; |
| 60 | + |
| 61 | +export function useCreate<%= name %>Service() { |
| 62 | + const fetch = useFetch(); |
| 63 | + |
| 64 | + return useCallback( |
| 65 | + (data: Create<%= name %>Request, requestConfig?: RequestConfigType) => { |
| 66 | + return fetch(`${API_URL}/v1/<%= h.inflection.transform(name, ['pluralize', 'underscore', 'dasherize']) %>`, { |
| 67 | + method: "POST", |
| 68 | + body: JSON.stringify(data), |
| 69 | + ...requestConfig, |
| 70 | + }).then(wrapperFetchJsonResponse<Create<%= name %>Response>); |
| 71 | + }, |
| 72 | + [fetch] |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +export type Edit<%= name %>Request = { |
| 77 | + id: Entity["id"]; |
| 78 | + data: Partial<Pick<Entity, "description">>; |
| 79 | +}; |
| 80 | + |
| 81 | +export type Edit<%= name %>Response = Entity; |
| 82 | + |
| 83 | +export function useEdit<%= name %>Service() { |
| 84 | + const fetch = useFetch(); |
| 85 | + |
| 86 | + return useCallback( |
| 87 | + (data: Edit<%= name %>Request, requestConfig?: RequestConfigType) => { |
| 88 | + return fetch(`${API_URL}/v1/<%= h.inflection.transform(name, ['pluralize', 'underscore', 'dasherize']) %>/${data.id}`, { |
| 89 | + method: "PATCH", |
| 90 | + body: JSON.stringify(data.data), |
| 91 | + ...requestConfig, |
| 92 | + }).then(wrapperFetchJsonResponse<Edit<%= name %>Response>); |
| 93 | + }, |
| 94 | + [fetch] |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +export type Delete<%= name %>Request = { |
| 99 | + id: Entity["id"]; |
| 100 | +}; |
| 101 | + |
| 102 | +export type Delete<%= name %>Response = undefined; |
| 103 | + |
| 104 | +export function useDelete<%= name %>Service() { |
| 105 | + const fetch = useFetch(); |
| 106 | + |
| 107 | + return useCallback( |
| 108 | + (data: Delete<%= name %>Request, requestConfig?: RequestConfigType) => { |
| 109 | + return fetch(`${API_URL}/v1/<%= h.inflection.transform(name, ['pluralize', 'underscore', 'dasherize']) %>/${data.id}`, { |
| 110 | + method: "DELETE", |
| 111 | + ...requestConfig, |
| 112 | + }).then(wrapperFetchJsonResponse<Delete<%= name %>Response>); |
| 113 | + }, |
| 114 | + [fetch] |
| 115 | + ); |
| 116 | +} |
0 commit comments