Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding support for response headers #35

Merged
merged 11 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,29 @@ export interface FetcherOptions<TQueryParams = never, TBody = never, THeaderPara

const JSON_HEADERS = ['application/json'];

export interface PaginationHeaders {
vardanbansal-harness marked this conversation as resolved.
Show resolved Hide resolved
total?: number | null;
totalPages?: number | null;
perPage?: number | null;
nextPage?: number | null;
prevPage?: number | null;
}

interface ResponseContainer<TResponse> {
content: TResponse;
headers: {
pagination: PaginationHeaders;
};
}

vardanbansal-harness marked this conversation as resolved.
Show resolved Hide resolved
export async function fetcher<
TResponse = unknown,
TQueryParams = never,
TBody = never,
THeaderParams = HeadersInit,
>(options: FetcherOptions<TQueryParams, TBody, THeaderParams>): Promise<TResponse> {
>(
options: FetcherOptions<TQueryParams, TBody, THeaderParams>,
): Promise<ResponseContainer<TResponse>> {
const { body, url, queryParams, headers, ...rest } = options;

const response = await fetch(url, {
Expand Down
13 changes: 13 additions & 0 deletions examples/oats.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ export default defineConfig({
output: './output/petstore-swagger',
url: 'https://petstore.swagger.io/v2/swagger.json',
fileHeader: '/* This is a sample header */',
genOnlyUsed: true,
plugins: [
reactQueryPlugin({
customFetcher: '../../../../custom-fetcher/index.js',
}),
],
},
'petstore-openapi-v3.0': {
output: './output/petstore-openapi-v3.0',
file: './schemas/petstore-v3.yaml',
genOnlyUsed: true,
plugins: [
reactQueryPlugin({
customFetcher: '../../../../custom-fetcher/index.js',
}),
],
},
github: {
url: 'https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.yaml',
Expand All @@ -20,6 +32,7 @@ export default defineConfig({
plugins: [
reactQueryPlugin({
allowedOperationIds: ['repos/list-for-authenticated-user'],
customFetcher: '../../../../custom-fetcher/index.js',
}),
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { NotModifiedResponse } from '../responses/NotModifiedResponse';
import type { RequiresAuthenticationResponse } from '../responses/RequiresAuthenticationResponse';
import type { ForbiddenResponse } from '../responses/ForbiddenResponse';
import type { ValidationFailedResponse } from '../responses/ValidationFailedResponse';
import { fetcher, FetcherOptions } from './fetcher';
import { fetcher, FetcherOptions } from '../../../../custom-fetcher/index.js';

export interface ReposListForAuthenticatedUserQueryQueryParams {
/**
Expand Down Expand Up @@ -59,9 +59,14 @@ export interface ReposListForAuthenticatedUserProps
queryParams: ReposListForAuthenticatedUserQueryQueryParams;
}

export interface ReposListForAuthenticatedUserResponseContainer {
content: ReposListForAuthenticatedUserOkResponse;
headers: Record<string, any>;
}

export function reposListForAuthenticatedUser(
props: ReposListForAuthenticatedUserProps,
): Promise<ReposListForAuthenticatedUserOkResponse> {
): Promise<ReposListForAuthenticatedUserResponseContainer> {
return fetcher<
ReposListForAuthenticatedUserOkResponse,
ReposListForAuthenticatedUserQueryQueryParams,
Expand All @@ -82,14 +87,14 @@ export function useReposListForAuthenticatedUserQuery(
props: ReposListForAuthenticatedUserProps,
options?: Omit<
UseQueryOptions<
ReposListForAuthenticatedUserOkResponse,
ReposListForAuthenticatedUserResponseContainer,
ReposListForAuthenticatedUserErrorResponse
>,
'queryKey' | 'queryFn'
>,
) {
return useQuery<
ReposListForAuthenticatedUserOkResponse,
ReposListForAuthenticatedUserResponseContainer,
ReposListForAuthenticatedUserErrorResponse
>(
['repos/list-for-authenticated-user', props.queryParams],
Expand Down
1 change: 0 additions & 1 deletion examples/output/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export type { RequiresAuthenticationResponse } from './responses/RequiresAuthent
export type { ValidationFailedResponse } from './responses/ValidationFailedResponse';
export type { BasicError } from './schemas/BasicError';
export type { NullableLicenseSimple } from './schemas/NullableLicenseSimple';
export type { NullableSimpleUser } from './schemas/NullableSimpleUser';
export type { Repository } from './schemas/Repository';
export type { SimpleUser } from './schemas/SimpleUser';
export type { ValidationError } from './schemas/ValidationError';
93 changes: 0 additions & 93 deletions examples/output/github/schemas/NullableSimpleUser.ts

This file was deleted.

Loading
Loading