| |
| |
| |
| |
| |
| |
| |
| import { useMutation, useQuery } from "@tanstack/react-query"; |
| import type { |
| MutationFunction, |
| QueryFunction, |
| QueryKey, |
| UseMutationOptions, |
| UseMutationResult, |
| UseQueryOptions, |
| UseQueryResult, |
| } from "@tanstack/react-query"; |
|
|
| import type { |
| ErrorResponse, |
| GenerateImageBody, |
| GenerateImageResponse, |
| GetImageHistoryParams, |
| HealthStatus, |
| ImageHistoryResponse, |
| SetTokenBody, |
| SuccessResponse, |
| TokenStatusResponse, |
| } from "./api.schemas"; |
|
|
| import { customFetch } from "../custom-fetch"; |
| import type { ErrorType, BodyType } from "../custom-fetch"; |
|
|
| type AwaitedInput<T> = PromiseLike<T> | T; |
|
|
| type Awaited<O> = O extends AwaitedInput<infer T> ? T : never; |
|
|
| type SecondParameter<T extends (...args: never) => unknown> = Parameters<T>[1]; |
|
|
| |
| |
| |
| |
| export const getHealthCheckUrl = () => { |
| return `/api/healthz`; |
| }; |
|
|
| export const healthCheck = async ( |
| options?: RequestInit, |
| ): Promise<HealthStatus> => { |
| return customFetch<HealthStatus>(getHealthCheckUrl(), { |
| ...options, |
| method: "GET", |
| }); |
| }; |
|
|
| export const getHealthCheckQueryKey = () => { |
| return [`/api/healthz`] as const; |
| }; |
|
|
| export const getHealthCheckQueryOptions = < |
| TData = Awaited<ReturnType<typeof healthCheck>>, |
| TError = ErrorType<unknown>, |
| >(options?: { |
| query?: UseQueryOptions< |
| Awaited<ReturnType<typeof healthCheck>>, |
| TError, |
| TData |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }) => { |
| const { query: queryOptions, request: requestOptions } = options ?? {}; |
|
|
| const queryKey = queryOptions?.queryKey ?? getHealthCheckQueryKey(); |
|
|
| const queryFn: QueryFunction<Awaited<ReturnType<typeof healthCheck>>> = ({ |
| signal, |
| }) => healthCheck({ signal, ...requestOptions }); |
|
|
| return { queryKey, queryFn, ...queryOptions } as UseQueryOptions< |
| Awaited<ReturnType<typeof healthCheck>>, |
| TError, |
| TData |
| > & { queryKey: QueryKey }; |
| }; |
|
|
| export type HealthCheckQueryResult = NonNullable< |
| Awaited<ReturnType<typeof healthCheck>> |
| >; |
| export type HealthCheckQueryError = ErrorType<unknown>; |
|
|
| |
| |
| |
|
|
| export function useHealthCheck< |
| TData = Awaited<ReturnType<typeof healthCheck>>, |
| TError = ErrorType<unknown>, |
| >(options?: { |
| query?: UseQueryOptions< |
| Awaited<ReturnType<typeof healthCheck>>, |
| TError, |
| TData |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseQueryResult<TData, TError> & { queryKey: QueryKey } { |
| const queryOptions = getHealthCheckQueryOptions(options); |
|
|
| const query = useQuery(queryOptions) as UseQueryResult<TData, TError> & { |
| queryKey: QueryKey; |
| }; |
|
|
| return { ...query, queryKey: queryOptions.queryKey }; |
| } |
|
|
| |
| |
| |
| |
| export const getGetConfigTokenUrl = () => { |
| return `/api/config/token`; |
| }; |
|
|
| export const getConfigToken = async ( |
| options?: RequestInit, |
| ): Promise<TokenStatusResponse> => { |
| return customFetch<TokenStatusResponse>(getGetConfigTokenUrl(), { |
| ...options, |
| method: "GET", |
| }); |
| }; |
|
|
| export const getGetConfigTokenQueryKey = () => { |
| return [`/api/config/token`] as const; |
| }; |
|
|
| export const getGetConfigTokenQueryOptions = < |
| TData = Awaited<ReturnType<typeof getConfigToken>>, |
| TError = ErrorType<unknown>, |
| >(options?: { |
| query?: UseQueryOptions< |
| Awaited<ReturnType<typeof getConfigToken>>, |
| TError, |
| TData |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }) => { |
| const { query: queryOptions, request: requestOptions } = options ?? {}; |
|
|
| const queryKey = queryOptions?.queryKey ?? getGetConfigTokenQueryKey(); |
|
|
| const queryFn: QueryFunction<Awaited<ReturnType<typeof getConfigToken>>> = ({ |
| signal, |
| }) => getConfigToken({ signal, ...requestOptions }); |
|
|
| return { queryKey, queryFn, ...queryOptions } as UseQueryOptions< |
| Awaited<ReturnType<typeof getConfigToken>>, |
| TError, |
| TData |
| > & { queryKey: QueryKey }; |
| }; |
|
|
| export type GetConfigTokenQueryResult = NonNullable< |
| Awaited<ReturnType<typeof getConfigToken>> |
| >; |
| export type GetConfigTokenQueryError = ErrorType<unknown>; |
|
|
| |
| |
| |
|
|
| export function useGetConfigToken< |
| TData = Awaited<ReturnType<typeof getConfigToken>>, |
| TError = ErrorType<unknown>, |
| >(options?: { |
| query?: UseQueryOptions< |
| Awaited<ReturnType<typeof getConfigToken>>, |
| TError, |
| TData |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseQueryResult<TData, TError> & { queryKey: QueryKey } { |
| const queryOptions = getGetConfigTokenQueryOptions(options); |
|
|
| const query = useQuery(queryOptions) as UseQueryResult<TData, TError> & { |
| queryKey: QueryKey; |
| }; |
|
|
| return { ...query, queryKey: queryOptions.queryKey }; |
| } |
|
|
| |
| |
| |
| |
| export const getSetConfigTokenUrl = () => { |
| return `/api/config/token`; |
| }; |
|
|
| export const setConfigToken = async ( |
| setTokenBody: SetTokenBody, |
| options?: RequestInit, |
| ): Promise<SuccessResponse> => { |
| return customFetch<SuccessResponse>(getSetConfigTokenUrl(), { |
| ...options, |
| method: "POST", |
| headers: { "Content-Type": "application/json", ...options?.headers }, |
| body: JSON.stringify(setTokenBody), |
| }); |
| }; |
|
|
| export const getSetConfigTokenMutationOptions = < |
| TError = ErrorType<unknown>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof setConfigToken>>, |
| TError, |
| { data: BodyType<SetTokenBody> }, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationOptions< |
| Awaited<ReturnType<typeof setConfigToken>>, |
| TError, |
| { data: BodyType<SetTokenBody> }, |
| TContext |
| > => { |
| const mutationKey = ["setConfigToken"]; |
| const { mutation: mutationOptions, request: requestOptions } = options |
| ? options.mutation && |
| "mutationKey" in options.mutation && |
| options.mutation.mutationKey |
| ? options |
| : { ...options, mutation: { ...options.mutation, mutationKey } } |
| : { mutation: { mutationKey }, request: undefined }; |
|
|
| const mutationFn: MutationFunction< |
| Awaited<ReturnType<typeof setConfigToken>>, |
| { data: BodyType<SetTokenBody> } |
| > = (props) => { |
| const { data } = props ?? {}; |
|
|
| return setConfigToken(data, requestOptions); |
| }; |
|
|
| return { mutationFn, ...mutationOptions }; |
| }; |
|
|
| export type SetConfigTokenMutationResult = NonNullable< |
| Awaited<ReturnType<typeof setConfigToken>> |
| >; |
| export type SetConfigTokenMutationBody = BodyType<SetTokenBody>; |
| export type SetConfigTokenMutationError = ErrorType<unknown>; |
|
|
| |
| |
| |
| export const useSetConfigToken = < |
| TError = ErrorType<unknown>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof setConfigToken>>, |
| TError, |
| { data: BodyType<SetTokenBody> }, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationResult< |
| Awaited<ReturnType<typeof setConfigToken>>, |
| TError, |
| { data: BodyType<SetTokenBody> }, |
| TContext |
| > => { |
| return useMutation(getSetConfigTokenMutationOptions(options)); |
| }; |
|
|
| |
| |
| |
| |
| export const getDeleteConfigTokenUrl = () => { |
| return `/api/config/token`; |
| }; |
|
|
| export const deleteConfigToken = async ( |
| options?: RequestInit, |
| ): Promise<SuccessResponse> => { |
| return customFetch<SuccessResponse>(getDeleteConfigTokenUrl(), { |
| ...options, |
| method: "DELETE", |
| }); |
| }; |
|
|
| export const getDeleteConfigTokenMutationOptions = < |
| TError = ErrorType<unknown>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof deleteConfigToken>>, |
| TError, |
| void, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationOptions< |
| Awaited<ReturnType<typeof deleteConfigToken>>, |
| TError, |
| void, |
| TContext |
| > => { |
| const mutationKey = ["deleteConfigToken"]; |
| const { mutation: mutationOptions, request: requestOptions } = options |
| ? options.mutation && |
| "mutationKey" in options.mutation && |
| options.mutation.mutationKey |
| ? options |
| : { ...options, mutation: { ...options.mutation, mutationKey } } |
| : { mutation: { mutationKey }, request: undefined }; |
|
|
| const mutationFn: MutationFunction< |
| Awaited<ReturnType<typeof deleteConfigToken>>, |
| void |
| > = () => { |
| return deleteConfigToken(requestOptions); |
| }; |
|
|
| return { mutationFn, ...mutationOptions }; |
| }; |
|
|
| export type DeleteConfigTokenMutationResult = NonNullable< |
| Awaited<ReturnType<typeof deleteConfigToken>> |
| >; |
|
|
| export type DeleteConfigTokenMutationError = ErrorType<unknown>; |
|
|
| |
| |
| |
| export const useDeleteConfigToken = < |
| TError = ErrorType<unknown>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof deleteConfigToken>>, |
| TError, |
| void, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationResult< |
| Awaited<ReturnType<typeof deleteConfigToken>>, |
| TError, |
| void, |
| TContext |
| > => { |
| return useMutation(getDeleteConfigTokenMutationOptions(options)); |
| }; |
|
|
| |
| |
| |
| |
| export const getGenerateImageUrl = () => { |
| return `/api/images/generate`; |
| }; |
|
|
| export const generateImage = async ( |
| generateImageBody: GenerateImageBody, |
| options?: RequestInit, |
| ): Promise<GenerateImageResponse> => { |
| return customFetch<GenerateImageResponse>(getGenerateImageUrl(), { |
| ...options, |
| method: "POST", |
| headers: { "Content-Type": "application/json", ...options?.headers }, |
| body: JSON.stringify(generateImageBody), |
| }); |
| }; |
|
|
| export const getGenerateImageMutationOptions = < |
| TError = ErrorType<ErrorResponse>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof generateImage>>, |
| TError, |
| { data: BodyType<GenerateImageBody> }, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationOptions< |
| Awaited<ReturnType<typeof generateImage>>, |
| TError, |
| { data: BodyType<GenerateImageBody> }, |
| TContext |
| > => { |
| const mutationKey = ["generateImage"]; |
| const { mutation: mutationOptions, request: requestOptions } = options |
| ? options.mutation && |
| "mutationKey" in options.mutation && |
| options.mutation.mutationKey |
| ? options |
| : { ...options, mutation: { ...options.mutation, mutationKey } } |
| : { mutation: { mutationKey }, request: undefined }; |
|
|
| const mutationFn: MutationFunction< |
| Awaited<ReturnType<typeof generateImage>>, |
| { data: BodyType<GenerateImageBody> } |
| > = (props) => { |
| const { data } = props ?? {}; |
|
|
| return generateImage(data, requestOptions); |
| }; |
|
|
| return { mutationFn, ...mutationOptions }; |
| }; |
|
|
| export type GenerateImageMutationResult = NonNullable< |
| Awaited<ReturnType<typeof generateImage>> |
| >; |
| export type GenerateImageMutationBody = BodyType<GenerateImageBody>; |
| export type GenerateImageMutationError = ErrorType<ErrorResponse>; |
|
|
| |
| |
| |
| export const useGenerateImage = < |
| TError = ErrorType<ErrorResponse>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof generateImage>>, |
| TError, |
| { data: BodyType<GenerateImageBody> }, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationResult< |
| Awaited<ReturnType<typeof generateImage>>, |
| TError, |
| { data: BodyType<GenerateImageBody> }, |
| TContext |
| > => { |
| return useMutation(getGenerateImageMutationOptions(options)); |
| }; |
|
|
| |
| |
| |
| |
| export const getGetImageHistoryUrl = (params?: GetImageHistoryParams) => { |
| const normalizedParams = new URLSearchParams(); |
|
|
| Object.entries(params || {}).forEach(([key, value]) => { |
| if (value !== undefined) { |
| normalizedParams.append(key, value === null ? "null" : value.toString()); |
| } |
| }); |
|
|
| const stringifiedParams = normalizedParams.toString(); |
|
|
| return stringifiedParams.length > 0 |
| ? `/api/images/history?${stringifiedParams}` |
| : `/api/images/history`; |
| }; |
|
|
| export const getImageHistory = async ( |
| params?: GetImageHistoryParams, |
| options?: RequestInit, |
| ): Promise<ImageHistoryResponse> => { |
| return customFetch<ImageHistoryResponse>(getGetImageHistoryUrl(params), { |
| ...options, |
| method: "GET", |
| }); |
| }; |
|
|
| export const getGetImageHistoryQueryKey = (params?: GetImageHistoryParams) => { |
| return [`/api/images/history`, ...(params ? [params] : [])] as const; |
| }; |
|
|
| export const getGetImageHistoryQueryOptions = < |
| TData = Awaited<ReturnType<typeof getImageHistory>>, |
| TError = ErrorType<unknown>, |
| >( |
| params?: GetImageHistoryParams, |
| options?: { |
| query?: UseQueryOptions< |
| Awaited<ReturnType<typeof getImageHistory>>, |
| TError, |
| TData |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }, |
| ) => { |
| const { query: queryOptions, request: requestOptions } = options ?? {}; |
|
|
| const queryKey = queryOptions?.queryKey ?? getGetImageHistoryQueryKey(params); |
|
|
| const queryFn: QueryFunction<Awaited<ReturnType<typeof getImageHistory>>> = ({ |
| signal, |
| }) => getImageHistory(params, { signal, ...requestOptions }); |
|
|
| return { queryKey, queryFn, ...queryOptions } as UseQueryOptions< |
| Awaited<ReturnType<typeof getImageHistory>>, |
| TError, |
| TData |
| > & { queryKey: QueryKey }; |
| }; |
|
|
| export type GetImageHistoryQueryResult = NonNullable< |
| Awaited<ReturnType<typeof getImageHistory>> |
| >; |
| export type GetImageHistoryQueryError = ErrorType<unknown>; |
|
|
| |
| |
| |
|
|
| export function useGetImageHistory< |
| TData = Awaited<ReturnType<typeof getImageHistory>>, |
| TError = ErrorType<unknown>, |
| >( |
| params?: GetImageHistoryParams, |
| options?: { |
| query?: UseQueryOptions< |
| Awaited<ReturnType<typeof getImageHistory>>, |
| TError, |
| TData |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }, |
| ): UseQueryResult<TData, TError> & { queryKey: QueryKey } { |
| const queryOptions = getGetImageHistoryQueryOptions(params, options); |
|
|
| const query = useQuery(queryOptions) as UseQueryResult<TData, TError> & { |
| queryKey: QueryKey; |
| }; |
|
|
| return { ...query, queryKey: queryOptions.queryKey }; |
| } |
|
|
| |
| |
| |
| |
| export const getDeleteImageUrl = (id: number) => { |
| return `/api/images/${id}`; |
| }; |
|
|
| export const deleteImage = async ( |
| id: number, |
| options?: RequestInit, |
| ): Promise<SuccessResponse> => { |
| return customFetch<SuccessResponse>(getDeleteImageUrl(id), { |
| ...options, |
| method: "DELETE", |
| }); |
| }; |
|
|
| export const getDeleteImageMutationOptions = < |
| TError = ErrorType<ErrorResponse>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof deleteImage>>, |
| TError, |
| { id: number }, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationOptions< |
| Awaited<ReturnType<typeof deleteImage>>, |
| TError, |
| { id: number }, |
| TContext |
| > => { |
| const mutationKey = ["deleteImage"]; |
| const { mutation: mutationOptions, request: requestOptions } = options |
| ? options.mutation && |
| "mutationKey" in options.mutation && |
| options.mutation.mutationKey |
| ? options |
| : { ...options, mutation: { ...options.mutation, mutationKey } } |
| : { mutation: { mutationKey }, request: undefined }; |
|
|
| const mutationFn: MutationFunction< |
| Awaited<ReturnType<typeof deleteImage>>, |
| { id: number } |
| > = (props) => { |
| const { id } = props ?? {}; |
|
|
| return deleteImage(id, requestOptions); |
| }; |
|
|
| return { mutationFn, ...mutationOptions }; |
| }; |
|
|
| export type DeleteImageMutationResult = NonNullable< |
| Awaited<ReturnType<typeof deleteImage>> |
| >; |
|
|
| export type DeleteImageMutationError = ErrorType<ErrorResponse>; |
|
|
| |
| |
| |
| export const useDeleteImage = < |
| TError = ErrorType<ErrorResponse>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof deleteImage>>, |
| TError, |
| { id: number }, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationResult< |
| Awaited<ReturnType<typeof deleteImage>>, |
| TError, |
| { id: number }, |
| TContext |
| > => { |
| return useMutation(getDeleteImageMutationOptions(options)); |
| }; |
|
|