Open History to see the last commit
1import { createClient, createConfig } from '@hey-api/client-next'
2import { queryOptions } from '@tanstack/react-query'
3β
4import type {
5 Client,
6 ClientOptions,
7 CreateClientConfig,
8 Options as HeyApiOptions,
9 TDataShape,
10} from '@hey-api/client-next'
11β
12export type Options<
13 TData extends TDataShape = TDataShape,
14 TThrowOnError extends boolean = boolean,
15> = HeyApiOptions<TData, TThrowOnError> & {
16 /**
17 * You can provide a client instance returned by `createClient()` instead of
18 * individual options. This might be also useful if you want to implement a
19 * custom client.
20 */
21 client?: Client
22 /**
23 * You can pass arbitrary values through the `meta` object. This can be
24 * used to access values that aren't defined as part of the SDK function.
25 */
26 meta?: Record<string, unknown>
27}
28β
29export const createClientConfig: CreateClientConfig = (config) => ({
30 ...config,
31 baseUrl: 'https://fakerestapi.azurewebsites.net/',
32})
33β
34export const _heyApiClient = createClient(
35 createClientConfig(
36 createConfig<ClientOptions>({
37 baseUrl: 'https://fakerestapi.azurewebsites.net',
38 }),
39 ),
40)
41β
42export type GetApiV1ActivitiesData = {
43 body?: never
44 path?: never
45 query?: never
46 url: '/api/v1/Activities'
47}
48β
49export type Activity = {
50 id?: number
51 title?: string | null
52 dueDate?: string
53 completed?: boolean
54}
55β
56export type Author = {
57 id?: number
58 idBook?: number
59 firstName?: string | null
60 lastName?: string | null
61}
62β
63export type GetApiV1ActivitiesResponses = {
64 /**
65 * Success
66 */
67 200: Array<Activity>
68}
69β
70export type GetApiV1AuthorsData = {
71 body?: never
72 path?: never
73 query?: never
74 url: '/api/v1/Authors'
75}
76β
77export type GetApiV1ActivitiesResponse =
78 GetApiV1ActivitiesResponses[keyof GetApiV1ActivitiesResponses]
79β
80export type QueryKey<TOptions extends Options> = [
81 Pick<TOptions, 'baseUrl' | 'body' | 'headers' | 'path' | 'query'> & {
82 _id: string
83 _infinite?: boolean
84 },
85]
86β
87const createQueryKey = <TOptions extends Options>(
88 id: string,
89 options?: TOptions,
90 infinite?: boolean,
91): [QueryKey<TOptions>[0]] => {
92 const params: QueryKey<TOptions>[0] = {
93 _id: id,
94 baseUrl: (options?.client ?? _heyApiClient).getConfig().baseUrl,
95 } as QueryKey<TOptions>[0]
96 if (infinite) {
97 params._infinite = infinite
98 }
99 if (options?.body) {
100 params.body = options.body
101 }
102 if (options?.headers) {
103 params.headers = options.headers
104 }
105 if (options?.path) {
106 params.path = options.path
107 }
108 if (options?.query) {
109 params.query = options.query
110 }
111 return [params]
112}
113β
114export const getApiV1Activities = <TThrowOnError extends boolean = false>(
115 options?: Options<GetApiV1ActivitiesData, TThrowOnError>,
116) => {
117 return (options?.client ?? _heyApiClient).get<
118 GetApiV1ActivitiesResponse,
119 unknown,
120 TThrowOnError
121 >({
122 url: '/api/v1/Activities',
123 ...options,
124 })
125}
126β
127export const getApiV1ActivitiesQueryKey = (
128 options?: Options<GetApiV1ActivitiesData>,
129) => createQueryKey('getApiV1Activities', options)
130β
131export const getApiV1ActivitiesOptions = (
132 options?: Options<GetApiV1ActivitiesData>,
133) => {
134 return queryOptions({
135 queryFn: async ({ queryKey, signal }) => {
136 const { data } = await getApiV1Activities({
137 ...options,
138 ...queryKey[0],
139 signal,
140 throwOnError: true,
141 })
142 return data
143 },
144 queryKey: getApiV1ActivitiesQueryKey(options),
145 })
146}
147β
148export type GetApiV1AuthorsResponses = {
149 /**
150 * Success
151 */
152 200: Array<Author>
153}
154β
155export type GetApiV1AuthorsResponse =
156 GetApiV1AuthorsResponses[keyof GetApiV1AuthorsResponses]
157β
158export const getApiV1Authors = <TThrowOnError extends boolean = false>(
159 options?: Options<GetApiV1AuthorsData, TThrowOnError>,
160) => {
161 return (options?.client ?? _heyApiClient).get<
162 GetApiV1AuthorsResponse,
163 unknown,
164 TThrowOnError
165 >({
166 url: '/api/v1/Authors',
167 ...options,
168 })
169}
170β
171export const getApiV1AuthorsQueryKey = (
172 options?: Options<GetApiV1AuthorsData>,
173) => createQueryKey('getApiV1Authors', options)
174β
175export const getApiV1AuthorsOptions = (
176 options?: Options<GetApiV1AuthorsData>,
177) => {
178 return queryOptions({
179 queryFn: async ({ queryKey, signal }) => {
180 const { data } = await getApiV1Authors({
181 ...options,
182 ...queryKey[0],
183 signal,
184 throwOnError: true,
185 })
186 return data
187 },
188 queryKey: getApiV1AuthorsQueryKey(options),
189 })
190}