feat(api): declare identity contract for registration, verification, and sessions
This commit is contained in:
1 parent
197661145e
commit
653f3775f8
86 files changed
+11826
-148
No files matched your search
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
* Generated by orval 🍺
|
||||
* Do not edit manually.
|
||||
* Lincodex API
|
||||
* OpenAPI spec version: 0.1.0
|
||||
*/
|
||||
import {
|
||||
faker
|
||||
} from '@faker-js/faker';
|
||||
|
||||
import {
|
||||
HttpResponse,
|
||||
http
|
||||
} from 'msw';
|
||||
import type {
|
||||
RequestHandlerOptions
|
||||
} from 'msw';
|
||||
|
||||
import {
|
||||
AccountScenario,
|
||||
ConsentPurpose,
|
||||
OnboardingStep
|
||||
} from '../../schemas';
|
||||
import type {
|
||||
Account,
|
||||
ConsentText,
|
||||
EmailVerificationResult,
|
||||
IdentityRequestAccepted,
|
||||
Session
|
||||
} from '../../schemas';
|
||||
|
||||
|
||||
export const getGetConsentTextResponseMock = (overrideResponse: Partial<Extract<ConsentText, object>> = {}): ConsentText => ({purpose: faker.helpers.arrayElement(Object.values(ConsentPurpose)), text_version: faker.helpers.fromRegExp("^[A-Za-z0-9._-]+$"), text: faker.string.alpha({length: {min: 1, max: 100000}}), published_at: faker.date.past().toISOString().slice(0, 19) + 'Z', ...overrideResponse})
|
||||
|
||||
export const getRegisterAccountResponseMock = (overrideResponse: Partial<Extract<IdentityRequestAccepted, object>> = {}): IdentityRequestAccepted => ({status: faker.helpers.arrayElement(['accepted'] as const), ...overrideResponse})
|
||||
|
||||
export const getResendEmailVerificationResponseMock = (overrideResponse: Partial<Extract<IdentityRequestAccepted, object>> = {}): IdentityRequestAccepted => ({status: faker.helpers.arrayElement(['accepted'] as const), ...overrideResponse})
|
||||
|
||||
export const getConfirmEmailResponseMock = (overrideResponse: Partial<Extract<EmailVerificationResult, object>> = {}): EmailVerificationResult => ({email_verified: faker.helpers.arrayElement([true] as const), scenario: faker.helpers.arrayElement(Object.values(AccountScenario)), onboarding_step: faker.helpers.arrayElement(Object.values(OnboardingStep)), ...overrideResponse})
|
||||
|
||||
export const getLoginResponseMock = (overrideResponse: Partial<Extract<Session, object>> = {}): Session => ({account: {id: faker.string.uuid(), email: faker.internet.email(), email_verified: faker.datatype.boolean(), scenario: faker.helpers.arrayElement(Object.values(AccountScenario)), onboarding_step: faker.helpers.arrayElement(Object.values(OnboardingStep)), created_at: faker.date.past().toISOString().slice(0, 19) + 'Z'}, csrf_token: faker.helpers.fromRegExp("^[A-Za-z0-9_-]+$"), ...overrideResponse})
|
||||
|
||||
export const getGetCurrentSessionResponseMock = (overrideResponse: Partial<Extract<Session, object>> = {}): Session => ({account: {id: faker.string.uuid(), email: faker.internet.email(), email_verified: faker.datatype.boolean(), scenario: faker.helpers.arrayElement(Object.values(AccountScenario)), onboarding_step: faker.helpers.arrayElement(Object.values(OnboardingStep)), created_at: faker.date.past().toISOString().slice(0, 19) + 'Z'}, csrf_token: faker.helpers.fromRegExp("^[A-Za-z0-9_-]+$"), ...overrideResponse})
|
||||
|
||||
export const getGetCurrentAccountResponseMock = (overrideResponse: Partial<Extract<Account, object>> = {}): Account => ({id: faker.string.uuid(), email: faker.internet.email(), email_verified: faker.datatype.boolean(), scenario: faker.helpers.arrayElement(Object.values(AccountScenario)), onboarding_step: faker.helpers.arrayElement(Object.values(OnboardingStep)), created_at: faker.date.past().toISOString().slice(0, 19) + 'Z', ...overrideResponse})
|
||||
|
||||
|
||||
export const getGetConsentTextMockHandler = (overrideResponse?: ConsentText | ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => Promise<ConsentText> | ConsentText), options?: RequestHandlerOptions) => {
|
||||
return http.get('*/api/v1/identity/consent-texts/:purpose', async (info: Parameters<Parameters<typeof http.get>[1]>[0]) => {
|
||||
|
||||
|
||||
return HttpResponse.json(overrideResponse !== undefined
|
||||
? (typeof overrideResponse === "function" ? await overrideResponse(info) : overrideResponse)
|
||||
: getGetConsentTextResponseMock(),
|
||||
{ status: 200
|
||||
})
|
||||
}, options)
|
||||
}
|
||||
|
||||
export const getRegisterAccountMockHandler = (overrideResponse?: IdentityRequestAccepted | ((info: Parameters<Parameters<typeof http.post>[1]>[0]) => Promise<IdentityRequestAccepted> | IdentityRequestAccepted), options?: RequestHandlerOptions) => {
|
||||
return http.post('*/api/v1/identity/registrations', async (info: Parameters<Parameters<typeof http.post>[1]>[0]) => {
|
||||
|
||||
|
||||
return HttpResponse.json(overrideResponse !== undefined
|
||||
? (typeof overrideResponse === "function" ? await overrideResponse(info) : overrideResponse)
|
||||
: getRegisterAccountResponseMock(),
|
||||
{ status: 202
|
||||
})
|
||||
}, options)
|
||||
}
|
||||
|
||||
export const getResendEmailVerificationMockHandler = (overrideResponse?: IdentityRequestAccepted | ((info: Parameters<Parameters<typeof http.post>[1]>[0]) => Promise<IdentityRequestAccepted> | IdentityRequestAccepted), options?: RequestHandlerOptions) => {
|
||||
return http.post('*/api/v1/identity/email-verification-requests', async (info: Parameters<Parameters<typeof http.post>[1]>[0]) => {
|
||||
|
||||
|
||||
return HttpResponse.json(overrideResponse !== undefined
|
||||
? (typeof overrideResponse === "function" ? await overrideResponse(info) : overrideResponse)
|
||||
: getResendEmailVerificationResponseMock(),
|
||||
{ status: 202
|
||||
})
|
||||
}, options)
|
||||
}
|
||||
|
||||
export const getConfirmEmailMockHandler = (overrideResponse?: EmailVerificationResult | ((info: Parameters<Parameters<typeof http.post>[1]>[0]) => Promise<EmailVerificationResult> | EmailVerificationResult), options?: RequestHandlerOptions) => {
|
||||
return http.post('*/api/v1/identity/email-verifications', async (info: Parameters<Parameters<typeof http.post>[1]>[0]) => {
|
||||
|
||||
|
||||
return HttpResponse.json(overrideResponse !== undefined
|
||||
? (typeof overrideResponse === "function" ? await overrideResponse(info) : overrideResponse)
|
||||
: getConfirmEmailResponseMock(),
|
||||
{ status: 200
|
||||
})
|
||||
}, options)
|
||||
}
|
||||
|
||||
export const getLoginMockHandler = (overrideResponse?: Session | ((info: Parameters<Parameters<typeof http.post>[1]>[0]) => Promise<Session> | Session), options?: RequestHandlerOptions) => {
|
||||
return http.post('*/api/v1/identity/sessions', async (info: Parameters<Parameters<typeof http.post>[1]>[0]) => {
|
||||
|
||||
|
||||
return HttpResponse.json(overrideResponse !== undefined
|
||||
? (typeof overrideResponse === "function" ? await overrideResponse(info) : overrideResponse)
|
||||
: getLoginResponseMock(),
|
||||
{ status: 201
|
||||
})
|
||||
}, options)
|
||||
}
|
||||
|
||||
export const getGetCurrentSessionMockHandler = (overrideResponse?: Session | ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => Promise<Session> | Session), options?: RequestHandlerOptions) => {
|
||||
return http.get('*/api/v1/identity/sessions/current', async (info: Parameters<Parameters<typeof http.get>[1]>[0]) => {
|
||||
|
||||
|
||||
return HttpResponse.json(overrideResponse !== undefined
|
||||
? (typeof overrideResponse === "function" ? await overrideResponse(info) : overrideResponse)
|
||||
: getGetCurrentSessionResponseMock(),
|
||||
{ status: 200
|
||||
})
|
||||
}, options)
|
||||
}
|
||||
|
||||
export const getLogoutMockHandler = (overrideResponse?: void | ((info: Parameters<Parameters<typeof http.delete>[1]>[0]) => Promise<void> | void), options?: RequestHandlerOptions) => {
|
||||
return http.delete('*/api/v1/identity/sessions/current', async (info: Parameters<Parameters<typeof http.delete>[1]>[0]) => {
|
||||
if (typeof overrideResponse === 'function') {await overrideResponse(info); }
|
||||
|
||||
return new HttpResponse(null,
|
||||
{ status: 204
|
||||
})
|
||||
}, options)
|
||||
}
|
||||
|
||||
export const getGetCurrentAccountMockHandler = (overrideResponse?: Account | ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => Promise<Account> | Account), options?: RequestHandlerOptions) => {
|
||||
return http.get('*/api/v1/identity/me', async (info: Parameters<Parameters<typeof http.get>[1]>[0]) => {
|
||||
|
||||
|
||||
return HttpResponse.json(overrideResponse !== undefined
|
||||
? (typeof overrideResponse === "function" ? await overrideResponse(info) : overrideResponse)
|
||||
: getGetCurrentAccountResponseMock(),
|
||||
{ status: 200
|
||||
})
|
||||
}, options)
|
||||
}
|
||||
export const getIdentityMock = () => [
|
||||
getGetConsentTextMockHandler(),
|
||||
getRegisterAccountMockHandler(),
|
||||
getResendEmailVerificationMockHandler(),
|
||||
getConfirmEmailMockHandler(),
|
||||
getLoginMockHandler(),
|
||||
getGetCurrentSessionMockHandler(),
|
||||
getLogoutMockHandler(),
|
||||
getGetCurrentAccountMockHandler()
|
||||
]
|
||||
@@ -0,0 +1,821 @@
|
||||
/**
|
||||
* Generated by orval 🍺
|
||||
* Do not edit manually.
|
||||
* Lincodex API
|
||||
* OpenAPI spec version: 0.1.0
|
||||
*/
|
||||
import {
|
||||
useMutation,
|
||||
useQuery
|
||||
} from '@tanstack/react-query';
|
||||
import type {
|
||||
DataTag,
|
||||
DefinedInitialDataOptions,
|
||||
DefinedUseQueryResult,
|
||||
MutationFunction,
|
||||
QueryClient,
|
||||
QueryFunction,
|
||||
QueryKey,
|
||||
UndefinedInitialDataOptions,
|
||||
UseMutationOptions,
|
||||
UseMutationResult,
|
||||
UseQueryOptions,
|
||||
UseQueryResult
|
||||
} from '@tanstack/react-query';
|
||||
|
||||
import type {
|
||||
Account,
|
||||
ConsentPurpose,
|
||||
ConsentText,
|
||||
EmailVerificationRequest,
|
||||
EmailVerificationResendRequest,
|
||||
EmailVerificationResult,
|
||||
IdentityRequestAccepted,
|
||||
LoginRequest,
|
||||
Problem,
|
||||
RegisterAccountHeaders,
|
||||
RegistrationRequest,
|
||||
Session
|
||||
} from '../../schemas';
|
||||
|
||||
import { apiRequest } from '../../client';
|
||||
import type { ErrorType } from '../../client';
|
||||
|
||||
|
||||
type SecondParameter<T extends (...args: never) => unknown> = Parameters<T>[1];
|
||||
|
||||
|
||||
|
||||
const withQueryKey = <T extends object, K>(query: T, queryKey: K): T & { queryKey: K } => {
|
||||
const result = { queryKey } as T & { queryKey: K };
|
||||
for (const key of Object.keys(query)) {
|
||||
// The explicit queryKey always wins, matching the previous
|
||||
// `{ ...query, queryKey }` spread where it was set last.
|
||||
if (key === 'queryKey') continue;
|
||||
Object.defineProperty(result, key, {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
get: () => (query as Record<string, unknown>)[key],
|
||||
});
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
export const getGetConsentTextUrl = (purpose: ConsentPurpose,) => {
|
||||
|
||||
|
||||
|
||||
|
||||
return `/api/v1/identity/consent-texts/${purpose}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Public operation without a session. Returns the current text of a consent that is given before an account exists; only `c1` is available. The registration page shows this text and sends its `text_version` with RegisterAccount, and the server hashes the same stored text when it records the acceptance. If the text changes while the page is open, RegisterAccount answers 400 validation_failed with the pointer `/consent_c1/text_version`, and the page reads the text again. An unknown purpose fails parameter validation with 400.
|
||||
* @summary Read the currently published text of a registration consent.
|
||||
*/
|
||||
export const getConsentText = async (purpose: ConsentPurpose, options?: Parameters<typeof apiRequest>[1]): Promise<ConsentText> => {
|
||||
|
||||
return apiRequest<ConsentText>(getGetConsentTextUrl(purpose),
|
||||
{
|
||||
...options,
|
||||
method: 'GET'
|
||||
|
||||
|
||||
}
|
||||
);}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export const getGetConsentTextQueryKey = (purpose: ConsentPurpose,) => {
|
||||
return [
|
||||
`/api/v1/identity/consent-texts/${purpose}`
|
||||
] as const;
|
||||
}
|
||||
|
||||
|
||||
export const getGetConsentTextQueryOptions = <TData = Awaited<ReturnType<typeof getConsentText>>, TError = ErrorType<Problem>>(purpose: ConsentPurpose, options?: { query?:Partial<UseQueryOptions<Awaited<ReturnType<typeof getConsentText>>, TError, TData>>, request?: SecondParameter<typeof apiRequest>}
|
||||
) => {
|
||||
|
||||
const {query: queryOptions, request: requestOptions} = options ?? {};
|
||||
|
||||
const queryKey = queryOptions?.queryKey ?? getGetConsentTextQueryKey(purpose);
|
||||
|
||||
|
||||
|
||||
const queryFn: QueryFunction<Awaited<ReturnType<typeof getConsentText>>> = ({ signal }) => getConsentText(purpose, { signal, ...requestOptions });
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return { queryKey, queryFn, enabled: purpose !== null && purpose !== undefined, ...queryOptions} as UseQueryOptions<Awaited<ReturnType<typeof getConsentText>>, TError, TData> & { queryKey: DataTag<QueryKey, TData, TError> }
|
||||
}
|
||||
|
||||
export type GetConsentTextQueryResult = NonNullable<Awaited<ReturnType<typeof getConsentText>>>
|
||||
export type GetConsentTextQueryError = ErrorType<Problem>
|
||||
|
||||
|
||||
export function useGetConsentText<TData = Awaited<ReturnType<typeof getConsentText>>, TError = ErrorType<Problem>>(
|
||||
purpose: ConsentPurpose, options: { query:Partial<UseQueryOptions<Awaited<ReturnType<typeof getConsentText>>, TError, TData>> & Pick<
|
||||
DefinedInitialDataOptions<
|
||||
Awaited<ReturnType<typeof getConsentText>>,
|
||||
TError,
|
||||
Awaited<ReturnType<typeof getConsentText>>
|
||||
> , 'initialData'
|
||||
>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient
|
||||
): DefinedUseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> }
|
||||
export function useGetConsentText<TData = Awaited<ReturnType<typeof getConsentText>>, TError = ErrorType<Problem>>(
|
||||
purpose: ConsentPurpose, options?: { query?:Partial<UseQueryOptions<Awaited<ReturnType<typeof getConsentText>>, TError, TData>> & Pick<
|
||||
UndefinedInitialDataOptions<
|
||||
Awaited<ReturnType<typeof getConsentText>>,
|
||||
TError,
|
||||
Awaited<ReturnType<typeof getConsentText>>
|
||||
> , 'initialData'
|
||||
>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient
|
||||
): UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> }
|
||||
export function useGetConsentText<TData = Awaited<ReturnType<typeof getConsentText>>, TError = ErrorType<Problem>>(
|
||||
purpose: ConsentPurpose, options?: { query?:Partial<UseQueryOptions<Awaited<ReturnType<typeof getConsentText>>, TError, TData>>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient
|
||||
): UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> }
|
||||
/**
|
||||
* @summary Read the currently published text of a registration consent.
|
||||
*/
|
||||
|
||||
export function useGetConsentText<TData = Awaited<ReturnType<typeof getConsentText>>, TError = ErrorType<Problem>>(
|
||||
purpose: ConsentPurpose, options?: { query?:Partial<UseQueryOptions<Awaited<ReturnType<typeof getConsentText>>, TError, TData>>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient
|
||||
): UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> } {
|
||||
|
||||
const queryOptions = getGetConsentTextQueryOptions(purpose,options)
|
||||
|
||||
const query = useQuery(queryOptions, queryClient) as UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> };
|
||||
|
||||
return withQueryKey(query, queryOptions.queryKey);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export const getRegisterAccountUrl = () => {
|
||||
|
||||
|
||||
|
||||
|
||||
return `/api/v1/identity/registrations`
|
||||
}
|
||||
|
||||
/**
|
||||
* Public operation without a session. The client reads the current C1 text with GetConsentText and sends the email, the password, the chosen scenario, and consent C1 with the version of the text the user accepted.
|
||||
*
|
||||
* The answer is the same 202 whether the email is new, registered but not verified, or verified, so registration does not enumerate accounts:
|
||||
*
|
||||
* - New email: the server creates an unverified account with the scenario and the first onboarding step, records C1 with its text version and text hash, stores only the hash of a one-time verification token, and queues the verification message, all in one transaction.
|
||||
* - Registered but not verified: the server treats the request as a new registration of that address. In one transaction it replaces the password hash, the scenario, the onboarding step, and the C1 record, advances the registration version, invalidates every earlier verification token, and queues a message with a new token. A pre-registration with someone else's password therefore never survives the address owner's own registration.
|
||||
* - Verified: nothing changes. The server may queue a rate-limited "you already have an account" notice to the address.
|
||||
*
|
||||
* Every verification token is bound to the registration version it was issued for, so a token issued before a re-registration can never confirm the newer credentials. An account that stays unverified is deleted with its consent record and tokens 7 days after its last registration (planned value from configuration). Verification tokens live 24 hours (planned value from configuration).
|
||||
*
|
||||
* The response does not depend on the branch in content or timing. Before it looks up the email, the server validates the whole body, including the password bounds after normalization, the password blocklist, and the C1 version, and it computes the Argon2id hash on every branch; a 400 therefore never depends on whether the email exists. Mail is delivered asynchronously from the outbox.
|
||||
*
|
||||
* The operation requires Idempotency-Key. For this anonymous operation the key is scoped by the operation, the key, and an HMAC of the normalized email, and the request fingerprint is an HMAC-SHA-256 with a server secret over the canonical JSON body. The server never stores the body, the password, or a plain hash of either; the secret keeps the fingerprint from serving as an offline password check. A retry with the same key, email, and body replays the stored 202 without a second message; the same key and email with a different body answers 422 idempotency_key_reused.
|
||||
*
|
||||
* It is rate-limited per client and per email and answers 429 with Retry-After when the limit is reached. Like every unsafe request, it requires an allowed Origin and answers 403 csrf_check_failed otherwise.
|
||||
* @summary Register an account by email with a scenario and consent C1.
|
||||
*/
|
||||
export const registerAccount = async (registrationRequest: RegistrationRequest,
|
||||
headers: RegisterAccountHeaders, options?: Parameters<typeof apiRequest>[1]): Promise<IdentityRequestAccepted> => {
|
||||
|
||||
const getHeaders = (h?: NonNullable<RequestInit['headers']>): Record<string, string | readonly string[]> => {
|
||||
if (!h) return {};
|
||||
if (h instanceof Headers) return Object.fromEntries(h.entries());
|
||||
if (Symbol.iterator in h) {
|
||||
return Object.fromEntries(
|
||||
Array.from(h as Iterable<Iterable<string>>, (entry) => Array.from(entry) as [string, string]),
|
||||
);
|
||||
}
|
||||
const headers: Record<string, string | readonly string[]> = {};
|
||||
for (const [name, value] of Object.entries<string | readonly string[] | undefined>(h)) {
|
||||
if (value !== undefined) headers[name] = value;
|
||||
}
|
||||
return headers;
|
||||
};
|
||||
return apiRequest<IdentityRequestAccepted>(getRegisterAccountUrl(),
|
||||
{
|
||||
...options,
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json',...headers, ...getHeaders(options?.headers) },
|
||||
body: JSON.stringify(registrationRequest)
|
||||
}
|
||||
);}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export const getRegisterAccountMutationKey = () => ['registerAccount'] as const;
|
||||
|
||||
export const getRegisterAccountMutationOptions = <TError = ErrorType<Problem>,
|
||||
TContext = unknown>(options?: { mutation?:UseMutationOptions<Awaited<ReturnType<typeof registerAccount>>, TError,RegisterAccountMutationVariables, TContext>, request?: SecondParameter<typeof apiRequest>}
|
||||
): UseMutationOptions<Awaited<ReturnType<typeof registerAccount>>, TError,RegisterAccountMutationVariables, TContext> => {
|
||||
|
||||
const mutationKey = getRegisterAccountMutationKey();
|
||||
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 registerAccount>>, RegisterAccountMutationVariables> = (props) => {
|
||||
const {data,headers} = props ?? {};
|
||||
|
||||
return registerAccount(data,headers,requestOptions)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return { mutationFn, ...mutationOptions }}
|
||||
|
||||
export type RegisterAccountMutationResult = NonNullable<Awaited<ReturnType<typeof registerAccount>>>
|
||||
export type RegisterAccountMutationBody = RegistrationRequest
|
||||
export type RegisterAccountMutationError = ErrorType<Problem>
|
||||
export type RegisterAccountMutationVariables = {data: RegistrationRequest;headers: RegisterAccountHeaders}
|
||||
|
||||
/**
|
||||
* @summary Register an account by email with a scenario and consent C1.
|
||||
*/
|
||||
export const useRegisterAccount = <TError = ErrorType<Problem>,
|
||||
TContext = unknown>(options?: { mutation?:UseMutationOptions<Awaited<ReturnType<typeof registerAccount>>, TError,RegisterAccountMutationVariables, TContext>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient): UseMutationResult<
|
||||
Awaited<ReturnType<typeof registerAccount>>,
|
||||
TError,
|
||||
RegisterAccountMutationVariables,
|
||||
TContext
|
||||
> => {
|
||||
return useMutation(getRegisterAccountMutationOptions(options), queryClient);
|
||||
}
|
||||
export const getResendEmailVerificationUrl = () => {
|
||||
|
||||
|
||||
|
||||
|
||||
return `/api/v1/identity/email-verification-requests`
|
||||
}
|
||||
|
||||
/**
|
||||
* Public operation without a session. The answer is the same 202 for an unknown email, a verified email, and an unverified one, so it does not enumerate accounts. Only for an unverified account does the server issue a new one-time token bound to the current registration version, invalidate every earlier token of that account, and queue the message. Resend never changes the password, the scenario, or the consent record; a user who did not start the pending registration registers again instead, which replaces it.
|
||||
*
|
||||
* It is rate-limited per client and per email and answers 429 with Retry-After when the limit is reached. A retry is safe without Idempotency-Key: at worst it replaces the token again within the rate limit. Like every unsafe request, it requires an allowed Origin and answers 403 csrf_check_failed otherwise.
|
||||
* @summary Request a new email verification link.
|
||||
*/
|
||||
export const resendEmailVerification = async (emailVerificationResendRequest: EmailVerificationResendRequest, options?: Parameters<typeof apiRequest>[1]): Promise<IdentityRequestAccepted> => {
|
||||
|
||||
const getHeaders = (h?: NonNullable<RequestInit['headers']>): Record<string, string | readonly string[]> => {
|
||||
if (!h) return {};
|
||||
if (h instanceof Headers) return Object.fromEntries(h.entries());
|
||||
if (Symbol.iterator in h) {
|
||||
return Object.fromEntries(
|
||||
Array.from(h as Iterable<Iterable<string>>, (entry) => Array.from(entry) as [string, string]),
|
||||
);
|
||||
}
|
||||
const headers: Record<string, string | readonly string[]> = {};
|
||||
for (const [name, value] of Object.entries<string | readonly string[] | undefined>(h)) {
|
||||
if (value !== undefined) headers[name] = value;
|
||||
}
|
||||
return headers;
|
||||
};
|
||||
return apiRequest<IdentityRequestAccepted>(getResendEmailVerificationUrl(),
|
||||
{
|
||||
...options,
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...getHeaders(options?.headers) },
|
||||
body: JSON.stringify(emailVerificationResendRequest)
|
||||
}
|
||||
);}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export const getResendEmailVerificationMutationKey = () => ['resendEmailVerification'] as const;
|
||||
|
||||
export const getResendEmailVerificationMutationOptions = <TError = ErrorType<Problem>,
|
||||
TContext = unknown>(options?: { mutation?:UseMutationOptions<Awaited<ReturnType<typeof resendEmailVerification>>, TError,ResendEmailVerificationMutationVariables, TContext>, request?: SecondParameter<typeof apiRequest>}
|
||||
): UseMutationOptions<Awaited<ReturnType<typeof resendEmailVerification>>, TError,ResendEmailVerificationMutationVariables, TContext> => {
|
||||
|
||||
const mutationKey = getResendEmailVerificationMutationKey();
|
||||
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 resendEmailVerification>>, ResendEmailVerificationMutationVariables> = (props) => {
|
||||
const {data} = props ?? {};
|
||||
|
||||
return resendEmailVerification(data,requestOptions)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return { mutationFn, ...mutationOptions }}
|
||||
|
||||
export type ResendEmailVerificationMutationResult = NonNullable<Awaited<ReturnType<typeof resendEmailVerification>>>
|
||||
export type ResendEmailVerificationMutationBody = EmailVerificationResendRequest
|
||||
export type ResendEmailVerificationMutationError = ErrorType<Problem>
|
||||
export type ResendEmailVerificationMutationVariables = {data: EmailVerificationResendRequest}
|
||||
|
||||
/**
|
||||
* @summary Request a new email verification link.
|
||||
*/
|
||||
export const useResendEmailVerification = <TError = ErrorType<Problem>,
|
||||
TContext = unknown>(options?: { mutation?:UseMutationOptions<Awaited<ReturnType<typeof resendEmailVerification>>, TError,ResendEmailVerificationMutationVariables, TContext>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient): UseMutationResult<
|
||||
Awaited<ReturnType<typeof resendEmailVerification>>,
|
||||
TError,
|
||||
ResendEmailVerificationMutationVariables,
|
||||
TContext
|
||||
> => {
|
||||
return useMutation(getResendEmailVerificationMutationOptions(options), queryClient);
|
||||
}
|
||||
export const getConfirmEmailUrl = () => {
|
||||
|
||||
|
||||
|
||||
|
||||
return `/api/v1/identity/email-verifications`
|
||||
}
|
||||
|
||||
/**
|
||||
* Public operation without a session. The verification link has the form `<frontend origin>/verify-email#token=<token>`: the token travels in the URL fragment, so it never reaches a server log or a Referer header. The page first removes the fragment with `history.replaceState`, then waits for an explicit user action, such as a Confirm button, before it posts the token here. A link scanner that only fetches the page therefore does not consume the token.
|
||||
*
|
||||
* The server looks up the token by its hash and, in one transaction, checks that it is unused, not expired, and bound to the current registration version, marks it used, and marks the email verified. Only one of several parallel requests with the same token succeeds. An expired token answers verification_token_expired; an unknown, used, replaced, or superseded token answers verification_token_invalid; neither changes anything. For both, the client offers a new link and sign-in, because the address may already be verified.
|
||||
*
|
||||
* A repeated confirmation with an already used token is not an idempotent 200: exactly one request may succeed for a token, so the outcome of parallel requests stays unambiguous, and the next step after either outcome is sign-in anyway.
|
||||
*
|
||||
* Success does not start a session: the link may be opened on another device or by a mail scanner, and a session must come only from the password. The response returns the scenario and the saved onboarding step so the client can continue after sign-in. The operation is rate-limited and requires an allowed Origin.
|
||||
* @summary Verify an email with the one-time token from the link.
|
||||
*/
|
||||
export const confirmEmail = async (emailVerificationRequest: EmailVerificationRequest, options?: Parameters<typeof apiRequest>[1]): Promise<EmailVerificationResult> => {
|
||||
|
||||
const getHeaders = (h?: NonNullable<RequestInit['headers']>): Record<string, string | readonly string[]> => {
|
||||
if (!h) return {};
|
||||
if (h instanceof Headers) return Object.fromEntries(h.entries());
|
||||
if (Symbol.iterator in h) {
|
||||
return Object.fromEntries(
|
||||
Array.from(h as Iterable<Iterable<string>>, (entry) => Array.from(entry) as [string, string]),
|
||||
);
|
||||
}
|
||||
const headers: Record<string, string | readonly string[]> = {};
|
||||
for (const [name, value] of Object.entries<string | readonly string[] | undefined>(h)) {
|
||||
if (value !== undefined) headers[name] = value;
|
||||
}
|
||||
return headers;
|
||||
};
|
||||
return apiRequest<EmailVerificationResult>(getConfirmEmailUrl(),
|
||||
{
|
||||
...options,
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...getHeaders(options?.headers) },
|
||||
body: JSON.stringify(emailVerificationRequest)
|
||||
}
|
||||
);}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export const getConfirmEmailMutationKey = () => ['confirmEmail'] as const;
|
||||
|
||||
export const getConfirmEmailMutationOptions = <TError = ErrorType<Problem>,
|
||||
TContext = unknown>(options?: { mutation?:UseMutationOptions<Awaited<ReturnType<typeof confirmEmail>>, TError,ConfirmEmailMutationVariables, TContext>, request?: SecondParameter<typeof apiRequest>}
|
||||
): UseMutationOptions<Awaited<ReturnType<typeof confirmEmail>>, TError,ConfirmEmailMutationVariables, TContext> => {
|
||||
|
||||
const mutationKey = getConfirmEmailMutationKey();
|
||||
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 confirmEmail>>, ConfirmEmailMutationVariables> = (props) => {
|
||||
const {data} = props ?? {};
|
||||
|
||||
return confirmEmail(data,requestOptions)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return { mutationFn, ...mutationOptions }}
|
||||
|
||||
export type ConfirmEmailMutationResult = NonNullable<Awaited<ReturnType<typeof confirmEmail>>>
|
||||
export type ConfirmEmailMutationBody = EmailVerificationRequest
|
||||
export type ConfirmEmailMutationError = ErrorType<Problem>
|
||||
export type ConfirmEmailMutationVariables = {data: EmailVerificationRequest}
|
||||
|
||||
/**
|
||||
* @summary Verify an email with the one-time token from the link.
|
||||
*/
|
||||
export const useConfirmEmail = <TError = ErrorType<Problem>,
|
||||
TContext = unknown>(options?: { mutation?:UseMutationOptions<Awaited<ReturnType<typeof confirmEmail>>, TError,ConfirmEmailMutationVariables, TContext>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient): UseMutationResult<
|
||||
Awaited<ReturnType<typeof confirmEmail>>,
|
||||
TError,
|
||||
ConfirmEmailMutationVariables,
|
||||
TContext
|
||||
> => {
|
||||
return useMutation(getConfirmEmailMutationOptions(options), queryClient);
|
||||
}
|
||||
export const getLoginUrl = () => {
|
||||
|
||||
|
||||
|
||||
|
||||
return `/api/v1/identity/sessions`
|
||||
}
|
||||
|
||||
/**
|
||||
* Public operation that starts a server session. The server verifies the password with Argon2id; for an unknown email it verifies against a fixed dummy hash, so the response time does not reveal whether the account exists. An unknown email and a wrong password both answer 401 invalid_credentials.
|
||||
*
|
||||
* Correct credentials for an unverified email answer 403 email_not_verified and create no session. This reveals the verification state only to someone who already knows the password, which proves more than the state itself; the client offers to resend the link.
|
||||
*
|
||||
* On success the server revokes the session that the request carried, if any, creates a new random session in PostgreSQL with a new identifier and a new CSRF token, sets the HttpOnly session cookie, and returns the account and the CSRF token. The new session follows the idle and absolute timeouts of the sessionCookie scheme. The client keeps the token in memory and sends it in X-CSRF-Token with every mutating request that requires a session.
|
||||
*
|
||||
* Login is rate-limited per client and per email and answers 429 with Retry-After. Like every unsafe request, it requires an allowed Origin, which protects against login CSRF, and answers 403 csrf_check_failed otherwise.
|
||||
* @summary Sign in with email and password and start a session.
|
||||
*/
|
||||
export const login = async (loginRequest: LoginRequest, options?: Parameters<typeof apiRequest>[1]): Promise<Session> => {
|
||||
|
||||
const getHeaders = (h?: NonNullable<RequestInit['headers']>): Record<string, string | readonly string[]> => {
|
||||
if (!h) return {};
|
||||
if (h instanceof Headers) return Object.fromEntries(h.entries());
|
||||
if (Symbol.iterator in h) {
|
||||
return Object.fromEntries(
|
||||
Array.from(h as Iterable<Iterable<string>>, (entry) => Array.from(entry) as [string, string]),
|
||||
);
|
||||
}
|
||||
const headers: Record<string, string | readonly string[]> = {};
|
||||
for (const [name, value] of Object.entries<string | readonly string[] | undefined>(h)) {
|
||||
if (value !== undefined) headers[name] = value;
|
||||
}
|
||||
return headers;
|
||||
};
|
||||
return apiRequest<Session>(getLoginUrl(),
|
||||
{
|
||||
...options,
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...getHeaders(options?.headers) },
|
||||
body: JSON.stringify(loginRequest)
|
||||
}
|
||||
);}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export const getLoginMutationKey = () => ['login'] as const;
|
||||
|
||||
export const getLoginMutationOptions = <TError = ErrorType<Problem>,
|
||||
TContext = unknown>(options?: { mutation?:UseMutationOptions<Awaited<ReturnType<typeof login>>, TError,LoginMutationVariables, TContext>, request?: SecondParameter<typeof apiRequest>}
|
||||
): UseMutationOptions<Awaited<ReturnType<typeof login>>, TError,LoginMutationVariables, TContext> => {
|
||||
|
||||
const mutationKey = getLoginMutationKey();
|
||||
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 login>>, LoginMutationVariables> = (props) => {
|
||||
const {data} = props ?? {};
|
||||
|
||||
return login(data,requestOptions)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return { mutationFn, ...mutationOptions }}
|
||||
|
||||
export type LoginMutationResult = NonNullable<Awaited<ReturnType<typeof login>>>
|
||||
export type LoginMutationBody = LoginRequest
|
||||
export type LoginMutationError = ErrorType<Problem>
|
||||
export type LoginMutationVariables = {data: LoginRequest}
|
||||
|
||||
/**
|
||||
* @summary Sign in with email and password and start a session.
|
||||
*/
|
||||
export const useLogin = <TError = ErrorType<Problem>,
|
||||
TContext = unknown>(options?: { mutation?:UseMutationOptions<Awaited<ReturnType<typeof login>>, TError,LoginMutationVariables, TContext>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient): UseMutationResult<
|
||||
Awaited<ReturnType<typeof login>>,
|
||||
TError,
|
||||
LoginMutationVariables,
|
||||
TContext
|
||||
> => {
|
||||
return useMutation(getLoginMutationOptions(options), queryClient);
|
||||
}
|
||||
export const getGetCurrentSessionUrl = () => {
|
||||
|
||||
|
||||
|
||||
|
||||
return `/api/v1/identity/sessions/current`
|
||||
}
|
||||
|
||||
/**
|
||||
* Requires the session cookie. Returns the account and the CSRF token of the current session, so the client can restore the token after a page reload or after 403 csrf_check_failed. A missing, unknown, expired, or revoked session answers 401 unauthenticated. Cross-origin pages cannot read the token, because the API sends no Access-Control-Allow-Origin header.
|
||||
* @summary Read the current session and its CSRF token.
|
||||
*/
|
||||
export const getCurrentSession = async ( options?: Parameters<typeof apiRequest>[1]): Promise<Session> => {
|
||||
|
||||
return apiRequest<Session>(getGetCurrentSessionUrl(),
|
||||
{
|
||||
...options,
|
||||
method: 'GET'
|
||||
|
||||
|
||||
}
|
||||
);}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export const getGetCurrentSessionQueryKey = () => {
|
||||
return [
|
||||
`/api/v1/identity/sessions/current`
|
||||
] as const;
|
||||
}
|
||||
|
||||
|
||||
export const getGetCurrentSessionQueryOptions = <TData = Awaited<ReturnType<typeof getCurrentSession>>, TError = ErrorType<Problem>>( options?: { query?:Partial<UseQueryOptions<Awaited<ReturnType<typeof getCurrentSession>>, TError, TData>>, request?: SecondParameter<typeof apiRequest>}
|
||||
) => {
|
||||
|
||||
const {query: queryOptions, request: requestOptions} = options ?? {};
|
||||
|
||||
const queryKey = queryOptions?.queryKey ?? getGetCurrentSessionQueryKey();
|
||||
|
||||
|
||||
|
||||
const queryFn: QueryFunction<Awaited<ReturnType<typeof getCurrentSession>>> = ({ signal }) => getCurrentSession({ signal, ...requestOptions });
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return { queryKey, queryFn, ...queryOptions} as UseQueryOptions<Awaited<ReturnType<typeof getCurrentSession>>, TError, TData> & { queryKey: DataTag<QueryKey, TData, TError> }
|
||||
}
|
||||
|
||||
export type GetCurrentSessionQueryResult = NonNullable<Awaited<ReturnType<typeof getCurrentSession>>>
|
||||
export type GetCurrentSessionQueryError = ErrorType<Problem>
|
||||
|
||||
|
||||
export function useGetCurrentSession<TData = Awaited<ReturnType<typeof getCurrentSession>>, TError = ErrorType<Problem>>(
|
||||
options: { query:Partial<UseQueryOptions<Awaited<ReturnType<typeof getCurrentSession>>, TError, TData>> & Pick<
|
||||
DefinedInitialDataOptions<
|
||||
Awaited<ReturnType<typeof getCurrentSession>>,
|
||||
TError,
|
||||
Awaited<ReturnType<typeof getCurrentSession>>
|
||||
> , 'initialData'
|
||||
>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient
|
||||
): DefinedUseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> }
|
||||
export function useGetCurrentSession<TData = Awaited<ReturnType<typeof getCurrentSession>>, TError = ErrorType<Problem>>(
|
||||
options?: { query?:Partial<UseQueryOptions<Awaited<ReturnType<typeof getCurrentSession>>, TError, TData>> & Pick<
|
||||
UndefinedInitialDataOptions<
|
||||
Awaited<ReturnType<typeof getCurrentSession>>,
|
||||
TError,
|
||||
Awaited<ReturnType<typeof getCurrentSession>>
|
||||
> , 'initialData'
|
||||
>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient
|
||||
): UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> }
|
||||
export function useGetCurrentSession<TData = Awaited<ReturnType<typeof getCurrentSession>>, TError = ErrorType<Problem>>(
|
||||
options?: { query?:Partial<UseQueryOptions<Awaited<ReturnType<typeof getCurrentSession>>, TError, TData>>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient
|
||||
): UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> }
|
||||
/**
|
||||
* @summary Read the current session and its CSRF token.
|
||||
*/
|
||||
|
||||
export function useGetCurrentSession<TData = Awaited<ReturnType<typeof getCurrentSession>>, TError = ErrorType<Problem>>(
|
||||
options?: { query?:Partial<UseQueryOptions<Awaited<ReturnType<typeof getCurrentSession>>, TError, TData>>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient
|
||||
): UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> } {
|
||||
|
||||
const queryOptions = getGetCurrentSessionQueryOptions(options)
|
||||
|
||||
const query = useQuery(queryOptions, queryClient) as UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> };
|
||||
|
||||
return withQueryKey(query, queryOptions.queryKey);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export const getLogoutUrl = () => {
|
||||
|
||||
|
||||
|
||||
|
||||
return `/api/v1/identity/sessions/current`
|
||||
}
|
||||
|
||||
/**
|
||||
* Requires the session cookie and the CSRF token of that session in X-CSRF-Token, and an allowed Origin. The server revokes the session in PostgreSQL, so the cookie value stops working even if a copy remains, and clears the cookie. A missing, expired, or revoked session answers 401 unauthenticated and also clears the cookie, so a stale cookie does not stay in the browser; a missing or wrong CSRF token or a disallowed Origin answers 403 csrf_check_failed and keeps the session and the cookie. The client clears its private query cache after the response.
|
||||
* @summary Sign out and revoke the current session.
|
||||
*/
|
||||
export const logout = async ( options?: Parameters<typeof apiRequest>[1]): Promise<void> => {
|
||||
|
||||
return apiRequest<void>(getLogoutUrl(),
|
||||
{
|
||||
...options,
|
||||
method: 'DELETE'
|
||||
|
||||
|
||||
}
|
||||
);}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export const getLogoutMutationKey = () => ['logout'] as const;
|
||||
|
||||
export const getLogoutMutationOptions = <TError = ErrorType<Problem>,
|
||||
TContext = unknown>(options?: { mutation?:UseMutationOptions<Awaited<ReturnType<typeof logout>>, TError,void, TContext>, request?: SecondParameter<typeof apiRequest>}
|
||||
): UseMutationOptions<Awaited<ReturnType<typeof logout>>, TError,void, TContext> => {
|
||||
|
||||
const mutationKey = getLogoutMutationKey();
|
||||
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 logout>>, void> = () => {
|
||||
|
||||
|
||||
return logout(requestOptions)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return { mutationFn, ...mutationOptions }}
|
||||
|
||||
export type LogoutMutationResult = NonNullable<Awaited<ReturnType<typeof logout>>>
|
||||
|
||||
export type LogoutMutationError = ErrorType<Problem>
|
||||
|
||||
|
||||
/**
|
||||
* @summary Sign out and revoke the current session.
|
||||
*/
|
||||
export const useLogout = <TError = ErrorType<Problem>,
|
||||
TContext = unknown>(options?: { mutation?:UseMutationOptions<Awaited<ReturnType<typeof logout>>, TError,void, TContext>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient): UseMutationResult<
|
||||
Awaited<ReturnType<typeof logout>>,
|
||||
TError,
|
||||
void,
|
||||
TContext
|
||||
> => {
|
||||
return useMutation(getLogoutMutationOptions(options), queryClient);
|
||||
}
|
||||
export const getGetCurrentAccountUrl = () => {
|
||||
|
||||
|
||||
|
||||
|
||||
return `/api/v1/identity/me`
|
||||
}
|
||||
|
||||
/**
|
||||
* Requires the session cookie. The account comes from the session, never from a request parameter. The client uses `scenario` and `onboarding_step` to return the user to the unfinished onboarding step. A missing, unknown, expired, or revoked session answers 401 unauthenticated. An unverified account never has a session, because login answers 403 email_not_verified for it, so this operation also answers 401 for such an account.
|
||||
* @summary Read the account of the current session.
|
||||
*/
|
||||
export const getCurrentAccount = async ( options?: Parameters<typeof apiRequest>[1]): Promise<Account> => {
|
||||
|
||||
return apiRequest<Account>(getGetCurrentAccountUrl(),
|
||||
{
|
||||
...options,
|
||||
method: 'GET'
|
||||
|
||||
|
||||
}
|
||||
);}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export const getGetCurrentAccountQueryKey = () => {
|
||||
return [
|
||||
`/api/v1/identity/me`
|
||||
] as const;
|
||||
}
|
||||
|
||||
|
||||
export const getGetCurrentAccountQueryOptions = <TData = Awaited<ReturnType<typeof getCurrentAccount>>, TError = ErrorType<Problem>>( options?: { query?:Partial<UseQueryOptions<Awaited<ReturnType<typeof getCurrentAccount>>, TError, TData>>, request?: SecondParameter<typeof apiRequest>}
|
||||
) => {
|
||||
|
||||
const {query: queryOptions, request: requestOptions} = options ?? {};
|
||||
|
||||
const queryKey = queryOptions?.queryKey ?? getGetCurrentAccountQueryKey();
|
||||
|
||||
|
||||
|
||||
const queryFn: QueryFunction<Awaited<ReturnType<typeof getCurrentAccount>>> = ({ signal }) => getCurrentAccount({ signal, ...requestOptions });
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return { queryKey, queryFn, ...queryOptions} as UseQueryOptions<Awaited<ReturnType<typeof getCurrentAccount>>, TError, TData> & { queryKey: DataTag<QueryKey, TData, TError> }
|
||||
}
|
||||
|
||||
export type GetCurrentAccountQueryResult = NonNullable<Awaited<ReturnType<typeof getCurrentAccount>>>
|
||||
export type GetCurrentAccountQueryError = ErrorType<Problem>
|
||||
|
||||
|
||||
export function useGetCurrentAccount<TData = Awaited<ReturnType<typeof getCurrentAccount>>, TError = ErrorType<Problem>>(
|
||||
options: { query:Partial<UseQueryOptions<Awaited<ReturnType<typeof getCurrentAccount>>, TError, TData>> & Pick<
|
||||
DefinedInitialDataOptions<
|
||||
Awaited<ReturnType<typeof getCurrentAccount>>,
|
||||
TError,
|
||||
Awaited<ReturnType<typeof getCurrentAccount>>
|
||||
> , 'initialData'
|
||||
>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient
|
||||
): DefinedUseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> }
|
||||
export function useGetCurrentAccount<TData = Awaited<ReturnType<typeof getCurrentAccount>>, TError = ErrorType<Problem>>(
|
||||
options?: { query?:Partial<UseQueryOptions<Awaited<ReturnType<typeof getCurrentAccount>>, TError, TData>> & Pick<
|
||||
UndefinedInitialDataOptions<
|
||||
Awaited<ReturnType<typeof getCurrentAccount>>,
|
||||
TError,
|
||||
Awaited<ReturnType<typeof getCurrentAccount>>
|
||||
> , 'initialData'
|
||||
>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient
|
||||
): UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> }
|
||||
export function useGetCurrentAccount<TData = Awaited<ReturnType<typeof getCurrentAccount>>, TError = ErrorType<Problem>>(
|
||||
options?: { query?:Partial<UseQueryOptions<Awaited<ReturnType<typeof getCurrentAccount>>, TError, TData>>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient
|
||||
): UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> }
|
||||
/**
|
||||
* @summary Read the account of the current session.
|
||||
*/
|
||||
|
||||
export function useGetCurrentAccount<TData = Awaited<ReturnType<typeof getCurrentAccount>>, TError = ErrorType<Problem>>(
|
||||
options?: { query?:Partial<UseQueryOptions<Awaited<ReturnType<typeof getCurrentAccount>>, TError, TData>>, request?: SecondParameter<typeof apiRequest>}
|
||||
, queryClient?: QueryClient
|
||||
): UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> } {
|
||||
|
||||
const queryOptions = getGetCurrentAccountQueryOptions(options)
|
||||
|
||||
const query = useQuery(queryOptions, queryClient) as UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> };
|
||||
|
||||
return withQueryKey(query, queryOptions.queryKey);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
export { getHealthMock } from './health/health.msw'
|
||||
export { getIdentityMock } from './identity/identity.msw'
|
||||
@@ -1 +1,2 @@
|
||||
export * from './health/health';
|
||||
export * from './identity/identity';
|
||||
Reference in new issue
Block a user