| |
| |
| |
| |
| |
| |
| |
| import { useMutation, useQuery } from "@tanstack/react-query"; |
| import type { |
| MutationFunction, |
| QueryFunction, |
| QueryKey, |
| UseMutationOptions, |
| UseMutationResult, |
| UseQueryOptions, |
| UseQueryResult, |
| } from "@tanstack/react-query"; |
|
|
| import type { |
| ActivateSubscriptionBody, |
| CreateSavedListBody, |
| CreateSubscriptionResponse, |
| HealthStatus, |
| ListSavedListsResponse, |
| MeResponse, |
| PaypalWebhookBody, |
| SavedList, |
| SubscriptionState, |
| } 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 getGetMeUrl = () => { |
| return `/api/me`; |
| }; |
|
|
| export const getMe = async (options?: RequestInit): Promise<MeResponse> => { |
| return customFetch<MeResponse>(getGetMeUrl(), { |
| ...options, |
| method: "GET", |
| }); |
| }; |
|
|
| export const getGetMeQueryKey = () => { |
| return [`/api/me`] as const; |
| }; |
|
|
| export const getGetMeQueryOptions = < |
| TData = Awaited<ReturnType<typeof getMe>>, |
| TError = ErrorType<void>, |
| >(options?: { |
| query?: UseQueryOptions<Awaited<ReturnType<typeof getMe>>, TError, TData>; |
| request?: SecondParameter<typeof customFetch>; |
| }) => { |
| const { query: queryOptions, request: requestOptions } = options ?? {}; |
|
|
| const queryKey = queryOptions?.queryKey ?? getGetMeQueryKey(); |
|
|
| const queryFn: QueryFunction<Awaited<ReturnType<typeof getMe>>> = ({ |
| signal, |
| }) => getMe({ signal, ...requestOptions }); |
|
|
| return { queryKey, queryFn, ...queryOptions } as UseQueryOptions< |
| Awaited<ReturnType<typeof getMe>>, |
| TError, |
| TData |
| > & { queryKey: QueryKey }; |
| }; |
|
|
| export type GetMeQueryResult = NonNullable<Awaited<ReturnType<typeof getMe>>>; |
| export type GetMeQueryError = ErrorType<void>; |
|
|
| |
| |
| |
|
|
| export function useGetMe< |
| TData = Awaited<ReturnType<typeof getMe>>, |
| TError = ErrorType<void>, |
| >(options?: { |
| query?: UseQueryOptions<Awaited<ReturnType<typeof getMe>>, TError, TData>; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseQueryResult<TData, TError> & { queryKey: QueryKey } { |
| const queryOptions = getGetMeQueryOptions(options); |
|
|
| const query = useQuery(queryOptions) as UseQueryResult<TData, TError> & { |
| queryKey: QueryKey; |
| }; |
|
|
| return { ...query, queryKey: queryOptions.queryKey }; |
| } |
|
|
| |
| |
| |
| export const getCreateSubscriptionUrl = () => { |
| return `/api/subscription/create`; |
| }; |
|
|
| export const createSubscription = async ( |
| options?: RequestInit, |
| ): Promise<CreateSubscriptionResponse> => { |
| return customFetch<CreateSubscriptionResponse>(getCreateSubscriptionUrl(), { |
| ...options, |
| method: "POST", |
| }); |
| }; |
|
|
| export const getCreateSubscriptionMutationOptions = < |
| TError = ErrorType<void>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof createSubscription>>, |
| TError, |
| void, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationOptions< |
| Awaited<ReturnType<typeof createSubscription>>, |
| TError, |
| void, |
| TContext |
| > => { |
| const mutationKey = ["createSubscription"]; |
| 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 createSubscription>>, |
| void |
| > = () => { |
| return createSubscription(requestOptions); |
| }; |
|
|
| return { mutationFn, ...mutationOptions }; |
| }; |
|
|
| export type CreateSubscriptionMutationResult = NonNullable< |
| Awaited<ReturnType<typeof createSubscription>> |
| >; |
|
|
| export type CreateSubscriptionMutationError = ErrorType<void>; |
|
|
| |
| |
| |
| export const useCreateSubscription = < |
| TError = ErrorType<void>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof createSubscription>>, |
| TError, |
| void, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationResult< |
| Awaited<ReturnType<typeof createSubscription>>, |
| TError, |
| void, |
| TContext |
| > => { |
| return useMutation(getCreateSubscriptionMutationOptions(options)); |
| }; |
|
|
| |
| |
| |
| export const getActivateSubscriptionUrl = () => { |
| return `/api/subscription/activate`; |
| }; |
|
|
| export const activateSubscription = async ( |
| activateSubscriptionBody: ActivateSubscriptionBody, |
| options?: RequestInit, |
| ): Promise<SubscriptionState> => { |
| return customFetch<SubscriptionState>(getActivateSubscriptionUrl(), { |
| ...options, |
| method: "POST", |
| headers: { "Content-Type": "application/json", ...options?.headers }, |
| body: JSON.stringify(activateSubscriptionBody), |
| }); |
| }; |
|
|
| export const getActivateSubscriptionMutationOptions = < |
| TError = ErrorType<unknown>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof activateSubscription>>, |
| TError, |
| { data: BodyType<ActivateSubscriptionBody> }, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationOptions< |
| Awaited<ReturnType<typeof activateSubscription>>, |
| TError, |
| { data: BodyType<ActivateSubscriptionBody> }, |
| TContext |
| > => { |
| const mutationKey = ["activateSubscription"]; |
| 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 activateSubscription>>, |
| { data: BodyType<ActivateSubscriptionBody> } |
| > = (props) => { |
| const { data } = props ?? {}; |
|
|
| return activateSubscription(data, requestOptions); |
| }; |
|
|
| return { mutationFn, ...mutationOptions }; |
| }; |
|
|
| export type ActivateSubscriptionMutationResult = NonNullable< |
| Awaited<ReturnType<typeof activateSubscription>> |
| >; |
| export type ActivateSubscriptionMutationBody = |
| BodyType<ActivateSubscriptionBody>; |
| export type ActivateSubscriptionMutationError = ErrorType<unknown>; |
|
|
| |
| |
| |
| export const useActivateSubscription = < |
| TError = ErrorType<unknown>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof activateSubscription>>, |
| TError, |
| { data: BodyType<ActivateSubscriptionBody> }, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationResult< |
| Awaited<ReturnType<typeof activateSubscription>>, |
| TError, |
| { data: BodyType<ActivateSubscriptionBody> }, |
| TContext |
| > => { |
| return useMutation(getActivateSubscriptionMutationOptions(options)); |
| }; |
|
|
| |
| |
| |
| export const getCancelSubscriptionUrl = () => { |
| return `/api/subscription/cancel`; |
| }; |
|
|
| export const cancelSubscription = async ( |
| options?: RequestInit, |
| ): Promise<SubscriptionState> => { |
| return customFetch<SubscriptionState>(getCancelSubscriptionUrl(), { |
| ...options, |
| method: "POST", |
| }); |
| }; |
|
|
| export const getCancelSubscriptionMutationOptions = < |
| TError = ErrorType<unknown>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof cancelSubscription>>, |
| TError, |
| void, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationOptions< |
| Awaited<ReturnType<typeof cancelSubscription>>, |
| TError, |
| void, |
| TContext |
| > => { |
| const mutationKey = ["cancelSubscription"]; |
| 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 cancelSubscription>>, |
| void |
| > = () => { |
| return cancelSubscription(requestOptions); |
| }; |
|
|
| return { mutationFn, ...mutationOptions }; |
| }; |
|
|
| export type CancelSubscriptionMutationResult = NonNullable< |
| Awaited<ReturnType<typeof cancelSubscription>> |
| >; |
|
|
| export type CancelSubscriptionMutationError = ErrorType<unknown>; |
|
|
| |
| |
| |
| export const useCancelSubscription = < |
| TError = ErrorType<unknown>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof cancelSubscription>>, |
| TError, |
| void, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationResult< |
| Awaited<ReturnType<typeof cancelSubscription>>, |
| TError, |
| void, |
| TContext |
| > => { |
| return useMutation(getCancelSubscriptionMutationOptions(options)); |
| }; |
|
|
| |
| |
| |
| export const getPaypalWebhookUrl = () => { |
| return `/api/paypal/webhook`; |
| }; |
|
|
| export const paypalWebhook = async ( |
| paypalWebhookBody: PaypalWebhookBody, |
| options?: RequestInit, |
| ): Promise<HealthStatus> => { |
| return customFetch<HealthStatus>(getPaypalWebhookUrl(), { |
| ...options, |
| method: "POST", |
| headers: { "Content-Type": "application/json", ...options?.headers }, |
| body: JSON.stringify(paypalWebhookBody), |
| }); |
| }; |
|
|
| export const getPaypalWebhookMutationOptions = < |
| TError = ErrorType<unknown>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof paypalWebhook>>, |
| TError, |
| { data: BodyType<PaypalWebhookBody> }, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationOptions< |
| Awaited<ReturnType<typeof paypalWebhook>>, |
| TError, |
| { data: BodyType<PaypalWebhookBody> }, |
| TContext |
| > => { |
| const mutationKey = ["paypalWebhook"]; |
| 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 paypalWebhook>>, |
| { data: BodyType<PaypalWebhookBody> } |
| > = (props) => { |
| const { data } = props ?? {}; |
|
|
| return paypalWebhook(data, requestOptions); |
| }; |
|
|
| return { mutationFn, ...mutationOptions }; |
| }; |
|
|
| export type PaypalWebhookMutationResult = NonNullable< |
| Awaited<ReturnType<typeof paypalWebhook>> |
| >; |
| export type PaypalWebhookMutationBody = BodyType<PaypalWebhookBody>; |
| export type PaypalWebhookMutationError = ErrorType<unknown>; |
|
|
| |
| |
| |
| export const usePaypalWebhook = < |
| TError = ErrorType<unknown>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof paypalWebhook>>, |
| TError, |
| { data: BodyType<PaypalWebhookBody> }, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationResult< |
| Awaited<ReturnType<typeof paypalWebhook>>, |
| TError, |
| { data: BodyType<PaypalWebhookBody> }, |
| TContext |
| > => { |
| return useMutation(getPaypalWebhookMutationOptions(options)); |
| }; |
|
|
| |
| |
| |
| export const getListSavedListsUrl = () => { |
| return `/api/saved-lists`; |
| }; |
|
|
| export const listSavedLists = async ( |
| options?: RequestInit, |
| ): Promise<ListSavedListsResponse> => { |
| return customFetch<ListSavedListsResponse>(getListSavedListsUrl(), { |
| ...options, |
| method: "GET", |
| }); |
| }; |
|
|
| export const getListSavedListsQueryKey = () => { |
| return [`/api/saved-lists`] as const; |
| }; |
|
|
| export const getListSavedListsQueryOptions = < |
| TData = Awaited<ReturnType<typeof listSavedLists>>, |
| TError = ErrorType<void>, |
| >(options?: { |
| query?: UseQueryOptions< |
| Awaited<ReturnType<typeof listSavedLists>>, |
| TError, |
| TData |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }) => { |
| const { query: queryOptions, request: requestOptions } = options ?? {}; |
|
|
| const queryKey = queryOptions?.queryKey ?? getListSavedListsQueryKey(); |
|
|
| const queryFn: QueryFunction<Awaited<ReturnType<typeof listSavedLists>>> = ({ |
| signal, |
| }) => listSavedLists({ signal, ...requestOptions }); |
|
|
| return { queryKey, queryFn, ...queryOptions } as UseQueryOptions< |
| Awaited<ReturnType<typeof listSavedLists>>, |
| TError, |
| TData |
| > & { queryKey: QueryKey }; |
| }; |
|
|
| export type ListSavedListsQueryResult = NonNullable< |
| Awaited<ReturnType<typeof listSavedLists>> |
| >; |
| export type ListSavedListsQueryError = ErrorType<void>; |
|
|
| |
| |
| |
|
|
| export function useListSavedLists< |
| TData = Awaited<ReturnType<typeof listSavedLists>>, |
| TError = ErrorType<void>, |
| >(options?: { |
| query?: UseQueryOptions< |
| Awaited<ReturnType<typeof listSavedLists>>, |
| TError, |
| TData |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseQueryResult<TData, TError> & { queryKey: QueryKey } { |
| const queryOptions = getListSavedListsQueryOptions(options); |
|
|
| const query = useQuery(queryOptions) as UseQueryResult<TData, TError> & { |
| queryKey: QueryKey; |
| }; |
|
|
| return { ...query, queryKey: queryOptions.queryKey }; |
| } |
|
|
| |
| |
| |
| export const getCreateSavedListUrl = () => { |
| return `/api/saved-lists`; |
| }; |
|
|
| export const createSavedList = async ( |
| createSavedListBody: CreateSavedListBody, |
| options?: RequestInit, |
| ): Promise<SavedList> => { |
| return customFetch<SavedList>(getCreateSavedListUrl(), { |
| ...options, |
| method: "POST", |
| headers: { "Content-Type": "application/json", ...options?.headers }, |
| body: JSON.stringify(createSavedListBody), |
| }); |
| }; |
|
|
| export const getCreateSavedListMutationOptions = < |
| TError = ErrorType<unknown>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof createSavedList>>, |
| TError, |
| { data: BodyType<CreateSavedListBody> }, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationOptions< |
| Awaited<ReturnType<typeof createSavedList>>, |
| TError, |
| { data: BodyType<CreateSavedListBody> }, |
| TContext |
| > => { |
| const mutationKey = ["createSavedList"]; |
| 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 createSavedList>>, |
| { data: BodyType<CreateSavedListBody> } |
| > = (props) => { |
| const { data } = props ?? {}; |
|
|
| return createSavedList(data, requestOptions); |
| }; |
|
|
| return { mutationFn, ...mutationOptions }; |
| }; |
|
|
| export type CreateSavedListMutationResult = NonNullable< |
| Awaited<ReturnType<typeof createSavedList>> |
| >; |
| export type CreateSavedListMutationBody = BodyType<CreateSavedListBody>; |
| export type CreateSavedListMutationError = ErrorType<unknown>; |
|
|
| |
| |
| |
| export const useCreateSavedList = < |
| TError = ErrorType<unknown>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof createSavedList>>, |
| TError, |
| { data: BodyType<CreateSavedListBody> }, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationResult< |
| Awaited<ReturnType<typeof createSavedList>>, |
| TError, |
| { data: BodyType<CreateSavedListBody> }, |
| TContext |
| > => { |
| return useMutation(getCreateSavedListMutationOptions(options)); |
| }; |
|
|
| |
| |
| |
| export const getDeleteSavedListUrl = (id: number) => { |
| return `/api/saved-lists/${id}`; |
| }; |
|
|
| export const deleteSavedList = async ( |
| id: number, |
| options?: RequestInit, |
| ): Promise<void> => { |
| return customFetch<void>(getDeleteSavedListUrl(id), { |
| ...options, |
| method: "DELETE", |
| }); |
| }; |
|
|
| export const getDeleteSavedListMutationOptions = < |
| TError = ErrorType<unknown>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof deleteSavedList>>, |
| TError, |
| { id: number }, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationOptions< |
| Awaited<ReturnType<typeof deleteSavedList>>, |
| TError, |
| { id: number }, |
| TContext |
| > => { |
| const mutationKey = ["deleteSavedList"]; |
| 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 deleteSavedList>>, |
| { id: number } |
| > = (props) => { |
| const { id } = props ?? {}; |
|
|
| return deleteSavedList(id, requestOptions); |
| }; |
|
|
| return { mutationFn, ...mutationOptions }; |
| }; |
|
|
| export type DeleteSavedListMutationResult = NonNullable< |
| Awaited<ReturnType<typeof deleteSavedList>> |
| >; |
|
|
| export type DeleteSavedListMutationError = ErrorType<unknown>; |
|
|
| |
| |
| |
| export const useDeleteSavedList = < |
| TError = ErrorType<unknown>, |
| TContext = unknown, |
| >(options?: { |
| mutation?: UseMutationOptions< |
| Awaited<ReturnType<typeof deleteSavedList>>, |
| TError, |
| { id: number }, |
| TContext |
| >; |
| request?: SecondParameter<typeof customFetch>; |
| }): UseMutationResult< |
| Awaited<ReturnType<typeof deleteSavedList>>, |
| TError, |
| { id: number }, |
| TContext |
| > => { |
| return useMutation(getDeleteSavedListMutationOptions(options)); |
| }; |
|
|