Skip to content

Commit

Permalink
Custom fetch som returnerer data: [] dersom 204
Browse files Browse the repository at this point in the history
  • Loading branch information
almyy committed Dec 11, 2024
1 parent a832b81 commit d6f5e6f
Show file tree
Hide file tree
Showing 18 changed files with 271 additions and 285 deletions.
4 changes: 4 additions & 0 deletions orval.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export default {
httpClient: "fetch",
mock: true,
override: {
mutator: {
path: "src/custom-fetch.ts",
name: "customFetch",
},
operations: {
harTilgang: {
fetch: {
Expand Down
15 changes: 15 additions & 0 deletions src/custom-fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const customFetch = async <T>(url: string, options: RequestInit): Promise<T> => {
const response = await fetch(url, options);
if (response.status === 204) {
return [] as T;
}
const data: T = await response.json();

console.log("custom fetch url: ", url);
if (url.includes("harTilgang")) {
console.log("returnerer custom boio", {data, status: response.status});
return {data, status: response.status} as T;
}

return data as T;
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import type {
UseQueryResult,
} from "@tanstack/react-query";
import type {FilOpplastingBody, OppdaterDigisosSakParams} from ".././model";
import {customFetch} from "../../custom-fetch";

type SecondParameter<T extends (...args: any) => any> = Parameters<T>[1];

export const getFilOpplastingUrl = (fiksDigisosId: string) => {
return `/sosialhjelp/innsyn/api/innsyn-api/api/v1/digisosapi/${fiksDigisosId}/filOpplasting`;
Expand All @@ -31,14 +34,11 @@ export const filOpplasting = async (
const formData = new FormData();
formData.append("file", filOpplastingBody.file);

const res = await fetch(getFilOpplastingUrl(fiksDigisosId), {
return customFetch<Promise<string>>(getFilOpplastingUrl(fiksDigisosId), {
...options,
method: "POST",
body: formData,
});
const data = await res.json();

return data as string;
};

export const getFilOpplastingMutationOptions = <TError = unknown, TContext = unknown>(options?: {
Expand All @@ -48,22 +48,22 @@ export const getFilOpplastingMutationOptions = <TError = unknown, TContext = unk
{fiksDigisosId: string; data: FilOpplastingBody},
TContext
>;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}): UseMutationOptions<
Awaited<ReturnType<typeof filOpplasting>>,
TError,
{fiksDigisosId: string; data: FilOpplastingBody},
TContext
> => {
const {mutation: mutationOptions, fetch: fetchOptions} = options ?? {};
const {mutation: mutationOptions, request: requestOptions} = options ?? {};

const mutationFn: MutationFunction<
Awaited<ReturnType<typeof filOpplasting>>,
{fiksDigisosId: string; data: FilOpplastingBody}
> = (props) => {
const {fiksDigisosId, data} = props ?? {};

return filOpplasting(fiksDigisosId, data, fetchOptions);
return filOpplasting(fiksDigisosId, data, requestOptions);
};

return {mutationFn, ...mutationOptions};
Expand All @@ -80,7 +80,7 @@ export const useFilOpplasting = <TError = unknown, TContext = unknown>(options?:
{fiksDigisosId: string; data: FilOpplastingBody},
TContext
>;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}): UseMutationResult<
Awaited<ReturnType<typeof filOpplasting>>,
TError,
Expand Down Expand Up @@ -110,15 +110,12 @@ export const oppdaterDigisosSak = async (
params?: OppdaterDigisosSakParams,
options?: RequestInit
): Promise<string> => {
const res = await fetch(getOppdaterDigisosSakUrl(params), {
return customFetch<Promise<string>>(getOppdaterDigisosSakUrl(params), {
...options,
method: "POST",
headers: {"Content-Type": "application/json", ...options?.headers},
body: JSON.stringify(oppdaterDigisosSakBody),
});
const data = await res.json();

return data as string;
};

export const getOppdaterDigisosSakMutationOptions = <TError = unknown, TContext = unknown>(options?: {
Expand All @@ -128,22 +125,22 @@ export const getOppdaterDigisosSakMutationOptions = <TError = unknown, TContext
{data: string; params?: OppdaterDigisosSakParams},
TContext
>;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}): UseMutationOptions<
Awaited<ReturnType<typeof oppdaterDigisosSak>>,
TError,
{data: string; params?: OppdaterDigisosSakParams},
TContext
> => {
const {mutation: mutationOptions, fetch: fetchOptions} = options ?? {};
const {mutation: mutationOptions, request: requestOptions} = options ?? {};

const mutationFn: MutationFunction<
Awaited<ReturnType<typeof oppdaterDigisosSak>>,
{data: string; params?: OppdaterDigisosSakParams}
> = (props) => {
const {data, params} = props ?? {};

return oppdaterDigisosSak(data, params, fetchOptions);
return oppdaterDigisosSak(data, params, requestOptions);
};

return {mutationFn, ...mutationOptions};
Expand All @@ -160,7 +157,7 @@ export const useOppdaterDigisosSak = <TError = unknown, TContext = unknown>(opti
{data: string; params?: OppdaterDigisosSakParams},
TContext
>;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}): UseMutationResult<
Awaited<ReturnType<typeof oppdaterDigisosSak>>,
TError,
Expand All @@ -176,13 +173,10 @@ export const getGetInnsynsfilUrl = (digisosId: string) => {
};

export const getInnsynsfil = async (digisosId: string, options?: RequestInit): Promise<string> => {
const res = await fetch(getGetInnsynsfilUrl(digisosId), {
return customFetch<Promise<string>>(getGetInnsynsfilUrl(digisosId), {
...options,
method: "GET",
});
const data = await res.json();

return data as string;
};

export const getGetInnsynsfilQueryKey = (digisosId: string) => {
Expand All @@ -193,15 +187,15 @@ export const getGetInnsynsfilQueryOptions = <TData = Awaited<ReturnType<typeof g
digisosId: string,
options?: {
query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof getInnsynsfil>>, TError, TData>>;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}
) => {
const {query: queryOptions, fetch: fetchOptions} = options ?? {};
const {query: queryOptions, request: requestOptions} = options ?? {};

const queryKey = queryOptions?.queryKey ?? getGetInnsynsfilQueryKey(digisosId);

const queryFn: QueryFunction<Awaited<ReturnType<typeof getInnsynsfil>>> = ({signal}) =>
getInnsynsfil(digisosId, {signal, ...fetchOptions});
getInnsynsfil(digisosId, {signal, ...requestOptions});

return {queryKey, queryFn, enabled: !!digisosId, ...queryOptions} as UseQueryOptions<
Awaited<ReturnType<typeof getInnsynsfil>>,
Expand All @@ -218,30 +212,30 @@ export function useGetInnsynsfil<TData = Awaited<ReturnType<typeof getInnsynsfil
options: {
query: Partial<UseQueryOptions<Awaited<ReturnType<typeof getInnsynsfil>>, TError, TData>> &
Pick<DefinedInitialDataOptions<Awaited<ReturnType<typeof getInnsynsfil>>, TError, TData>, "initialData">;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}
): DefinedUseQueryResult<TData, TError> & {queryKey: QueryKey};
export function useGetInnsynsfil<TData = Awaited<ReturnType<typeof getInnsynsfil>>, TError = unknown>(
digisosId: string,
options?: {
query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof getInnsynsfil>>, TError, TData>> &
Pick<UndefinedInitialDataOptions<Awaited<ReturnType<typeof getInnsynsfil>>, TError, TData>, "initialData">;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}
): UseQueryResult<TData, TError> & {queryKey: QueryKey};
export function useGetInnsynsfil<TData = Awaited<ReturnType<typeof getInnsynsfil>>, TError = unknown>(
digisosId: string,
options?: {
query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof getInnsynsfil>>, TError, TData>>;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}
): UseQueryResult<TData, TError> & {queryKey: QueryKey};

export function useGetInnsynsfil<TData = Awaited<ReturnType<typeof getInnsynsfil>>, TError = unknown>(
digisosId: string,
options?: {
query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof getInnsynsfil>>, TError, TData>>;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}
): UseQueryResult<TData, TError> & {queryKey: QueryKey} {
const queryOptions = getGetInnsynsfilQueryOptions(digisosId, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import type {
UseQueryResult,
} from "@tanstack/react-query";
import type {ForelopigSvarResponse} from ".././model";
import {customFetch} from "../../custom-fetch";

type SecondParameter<T extends (...args: any) => any> = Parameters<T>[1];

export const getHentForelopigSvarStatusUrl = (fiksDigisosId: string) => {
return `/sosialhjelp/innsyn/api/innsyn-api/api/v1/innsyn/${fiksDigisosId}/forelopigSvar`;
Expand All @@ -24,13 +27,10 @@ export const hentForelopigSvarStatus = async (
fiksDigisosId: string,
options?: RequestInit
): Promise<ForelopigSvarResponse> => {
const res = await fetch(getHentForelopigSvarStatusUrl(fiksDigisosId), {
return customFetch<Promise<ForelopigSvarResponse>>(getHentForelopigSvarStatusUrl(fiksDigisosId), {
...options,
method: "GET",
});
const data = await res.json();

return data as ForelopigSvarResponse;
};

export const getHentForelopigSvarStatusQueryKey = (fiksDigisosId: string) => {
Expand All @@ -44,15 +44,15 @@ export const getHentForelopigSvarStatusQueryOptions = <
fiksDigisosId: string,
options?: {
query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof hentForelopigSvarStatus>>, TError, TData>>;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}
) => {
const {query: queryOptions, fetch: fetchOptions} = options ?? {};
const {query: queryOptions, request: requestOptions} = options ?? {};

const queryKey = queryOptions?.queryKey ?? getHentForelopigSvarStatusQueryKey(fiksDigisosId);

const queryFn: QueryFunction<Awaited<ReturnType<typeof hentForelopigSvarStatus>>> = ({signal}) =>
hentForelopigSvarStatus(fiksDigisosId, {signal, ...fetchOptions});
hentForelopigSvarStatus(fiksDigisosId, {signal, ...requestOptions});

return {queryKey, queryFn, enabled: !!fiksDigisosId, ...queryOptions} as UseQueryOptions<
Awaited<ReturnType<typeof hentForelopigSvarStatus>>,
Expand All @@ -75,7 +75,7 @@ export function useHentForelopigSvarStatus<
DefinedInitialDataOptions<Awaited<ReturnType<typeof hentForelopigSvarStatus>>, TError, TData>,
"initialData"
>;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}
): DefinedUseQueryResult<TData, TError> & {queryKey: QueryKey};
export function useHentForelopigSvarStatus<
Expand All @@ -89,7 +89,7 @@ export function useHentForelopigSvarStatus<
UndefinedInitialDataOptions<Awaited<ReturnType<typeof hentForelopigSvarStatus>>, TError, TData>,
"initialData"
>;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}
): UseQueryResult<TData, TError> & {queryKey: QueryKey};
export function useHentForelopigSvarStatus<
Expand All @@ -99,7 +99,7 @@ export function useHentForelopigSvarStatus<
fiksDigisosId: string,
options?: {
query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof hentForelopigSvarStatus>>, TError, TData>>;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}
): UseQueryResult<TData, TError> & {queryKey: QueryKey};

Expand All @@ -110,7 +110,7 @@ export function useHentForelopigSvarStatus<
fiksDigisosId: string,
options?: {
query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof hentForelopigSvarStatus>>, TError, TData>>;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}
): UseQueryResult<TData, TError> & {queryKey: QueryKey} {
const queryOptions = getHentForelopigSvarStatusQueryOptions(fiksDigisosId, options);
Expand Down
22 changes: 11 additions & 11 deletions src/generated/hendelse-controller/hendelse-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ import type {
UseQueryResult,
} from "@tanstack/react-query";
import type {HendelseResponse} from ".././model";
import {customFetch} from "../../custom-fetch";

type SecondParameter<T extends (...args: any) => any> = Parameters<T>[1];

export const getHentHendelserUrl = (fiksDigisosId: string) => {
return `/sosialhjelp/innsyn/api/innsyn-api/api/v1/innsyn/${fiksDigisosId}/hendelser`;
};

export const hentHendelser = async (fiksDigisosId: string, options?: RequestInit): Promise<HendelseResponse[]> => {
const res = await fetch(getHentHendelserUrl(fiksDigisosId), {
return customFetch<Promise<HendelseResponse[]>>(getHentHendelserUrl(fiksDigisosId), {
...options,
method: "GET",
});
const data = await res.json();

return data as HendelseResponse[];
};

export const getHentHendelserQueryKey = (fiksDigisosId: string) => {
Expand All @@ -38,15 +38,15 @@ export const getHentHendelserQueryOptions = <TData = Awaited<ReturnType<typeof h
fiksDigisosId: string,
options?: {
query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof hentHendelser>>, TError, TData>>;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}
) => {
const {query: queryOptions, fetch: fetchOptions} = options ?? {};
const {query: queryOptions, request: requestOptions} = options ?? {};

const queryKey = queryOptions?.queryKey ?? getHentHendelserQueryKey(fiksDigisosId);

const queryFn: QueryFunction<Awaited<ReturnType<typeof hentHendelser>>> = ({signal}) =>
hentHendelser(fiksDigisosId, {signal, ...fetchOptions});
hentHendelser(fiksDigisosId, {signal, ...requestOptions});

return {queryKey, queryFn, enabled: !!fiksDigisosId, ...queryOptions} as UseQueryOptions<
Awaited<ReturnType<typeof hentHendelser>>,
Expand All @@ -63,30 +63,30 @@ export function useHentHendelser<TData = Awaited<ReturnType<typeof hentHendelser
options: {
query: Partial<UseQueryOptions<Awaited<ReturnType<typeof hentHendelser>>, TError, TData>> &
Pick<DefinedInitialDataOptions<Awaited<ReturnType<typeof hentHendelser>>, TError, TData>, "initialData">;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}
): DefinedUseQueryResult<TData, TError> & {queryKey: QueryKey};
export function useHentHendelser<TData = Awaited<ReturnType<typeof hentHendelser>>, TError = unknown>(
fiksDigisosId: string,
options?: {
query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof hentHendelser>>, TError, TData>> &
Pick<UndefinedInitialDataOptions<Awaited<ReturnType<typeof hentHendelser>>, TError, TData>, "initialData">;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}
): UseQueryResult<TData, TError> & {queryKey: QueryKey};
export function useHentHendelser<TData = Awaited<ReturnType<typeof hentHendelser>>, TError = unknown>(
fiksDigisosId: string,
options?: {
query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof hentHendelser>>, TError, TData>>;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}
): UseQueryResult<TData, TError> & {queryKey: QueryKey};

export function useHentHendelser<TData = Awaited<ReturnType<typeof hentHendelser>>, TError = unknown>(
fiksDigisosId: string,
options?: {
query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof hentHendelser>>, TError, TData>>;
fetch?: RequestInit;
request?: SecondParameter<typeof customFetch>;
}
): UseQueryResult<TData, TError> & {queryKey: QueryKey} {
const queryOptions = getHentHendelserQueryOptions(fiksDigisosId, options);
Expand Down
Loading

0 comments on commit d6f5e6f

Please sign in to comment.