Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
236 changes: 236 additions & 0 deletions apps/meteor/app/api/server/ApiClass.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,239 @@ it('Should return the expected type', () => {
>;
true as test;
});

describe('ExtractRoutesFromAPI GET when query is (never, unknown, any, e.g.)', () => {
it('Should extract correct function signature when query is not present', () => {
type APIWithNeverQuery = APIClass<
'/v1',
{
method: 'GET';
path: '/v1/endpoint.test';
response: {
200: ValidateFunction<unknown>;
};
authRequired: true;
}
>;
type ExpectedFunctionSignature = Expect<
ShallowEqual<ExtractRoutesFromAPI<APIWithNeverQuery>['/v1/endpoint.test']['GET'], (params: unknown) => unknown>
>;
true as ExpectedFunctionSignature;
});
Comment thread
ahmed-n-abdeltwab marked this conversation as resolved.
Outdated

it('Should extract correct function signature when query is never', () => {
type APIWithNeverQuery = APIClass<
'/v1',
{
method: 'GET';
path: '/v1/endpoint.test';
response: {
200: ValidateFunction<unknown>;
};
query: ValidateFunction<never>;
authRequired: true;
}
>;
type ExpectedFunctionSignature = Expect<
ShallowEqual<ExtractRoutesFromAPI<APIWithNeverQuery>['/v1/endpoint.test']['GET'], (params: never) => unknown>
>;
true as ExpectedFunctionSignature;
});

it('Should extract correct function signature when query is undefined', () => {
type APIWithUndefinedQuery = APIClass<
'/v1',
{
method: 'GET';
path: '/v1/endpoint.test';
response: {
200: ValidateFunction<unknown>;
};
query: ValidateFunction<undefined>;
authRequired: true;
}
>;
type ExpectedFunctionSignature = Expect<
ShallowEqual<ExtractRoutesFromAPI<APIWithUndefinedQuery>['/v1/endpoint.test']['GET'], (params: undefined) => unknown>
>;
true as ExpectedFunctionSignature;
});

it('Should extract correct function signature when query is any', () => {
type APIWithAnyQuery = APIClass<
'/v1',
{
method: 'GET';
path: '/v1/endpoint.test';
response: {
200: ValidateFunction<unknown>;
};
query: ValidateFunction<any>;
authRequired: true;
}
>;
type ExpectedFunctionSignature = Expect<
ShallowEqual<ExtractRoutesFromAPI<APIWithAnyQuery>['/v1/endpoint.test']['GET'], (params: unknown) => unknown>
>;
true as ExpectedFunctionSignature;
});

it('Should extract correct function signature when query is unknown', () => {
type APIWithUnknownQuery = APIClass<
'/v1',
{
method: 'GET';
path: '/v1/endpoint.test';
response: {
200: ValidateFunction<unknown>;
};
query: ValidateFunction<unknown>;
authRequired: true;
}
>;
type ExpectedFunctionSignature = Expect<
ShallowEqual<ExtractRoutesFromAPI<APIWithUnknownQuery>['/v1/endpoint.test']['GET'], (params: unknown) => unknown>
>;
true as ExpectedFunctionSignature;
});

it('Should extract correct function signature when query is null', () => {
type APIWithNullQuery = APIClass<
'/v1',
{
method: 'GET';
path: '/v1/endpoint.test';
response: {
200: ValidateFunction<unknown>;
};
query: ValidateFunction<null>;
authRequired: true;
}
>;
type ExpectedFunctionSignature = Expect<
ShallowEqual<ExtractRoutesFromAPI<APIWithNullQuery>['/v1/endpoint.test']['GET'], (params: null) => unknown>
>;
true as ExpectedFunctionSignature;
});
});

describe('ExtractRoutesFromAPI POST when body is (never, unknown, any, e.g.)', () => {
it('Should extract correct function signature when body is not present', () => {
type APIWithNeverBody = APIClass<
'/v1',
{
method: 'POST';
path: '/v1/endpoint.test';
response: {
200: ValidateFunction<unknown>;
201: ValidateFunction<unknown>;
};
authRequired: true;
}
>;
type ExpectedFunctionSignature = Expect<
ShallowEqual<ExtractRoutesFromAPI<APIWithNeverBody>['/v1/endpoint.test']['POST'], (params: unknown) => unknown>
>;
true as ExpectedFunctionSignature;
});

it('Should extract correct function signature when body is never', () => {
type APIWithNeverBody = APIClass<
'/v1',
{
method: 'POST';
path: '/v1/endpoint.test';
response: {
200: ValidateFunction<unknown>;
201: ValidateFunction<unknown>;
};
body: ValidateFunction<never>;
authRequired: true;
}
>;
type ExpectedFunctionSignature = Expect<
ShallowEqual<ExtractRoutesFromAPI<APIWithNeverBody>['/v1/endpoint.test']['POST'], (params: never) => unknown>
>;
true as ExpectedFunctionSignature;
});

it('Should extract correct function signature when body is undefined', () => {
type APIWithUndefinedBody = APIClass<
'/v1',
{
method: 'POST';
path: '/v1/endpoint.test';
response: {
200: ValidateFunction<unknown>;
201: ValidateFunction<unknown>;
};
body: ValidateFunction<undefined>;
authRequired: true;
}
>;
type ExpectedFunctionSignature = Expect<
ShallowEqual<ExtractRoutesFromAPI<APIWithUndefinedBody>['/v1/endpoint.test']['POST'], (params: undefined) => unknown>
>;
true as ExpectedFunctionSignature;
});

it('Should extract correct function signature when body is any', () => {
type APIWithAnyBody = APIClass<
'/v1',
{
method: 'POST';
path: '/v1/endpoint.test';
response: {
200: ValidateFunction<unknown>;
201: ValidateFunction<unknown>;
};
body: ValidateFunction<any>;
authRequired: true;
}
>;
type ExpectedFunctionSignature = Expect<
ShallowEqual<ExtractRoutesFromAPI<APIWithAnyBody>['/v1/endpoint.test']['POST'], (params: unknown) => unknown>
>;
true as ExpectedFunctionSignature;
});

it('Should extract correct function signature when body is unknown', () => {
type APIWithUnknownBody = APIClass<
'/v1',
{
method: 'POST';
path: '/v1/endpoint.test';
response: {
200: ValidateFunction<unknown>;
201: ValidateFunction<unknown>;
};
body: ValidateFunction<unknown>;
authRequired: true;
}
>;
type ExpectedFunctionSignature = Expect<
ShallowEqual<ExtractRoutesFromAPI<APIWithUnknownBody>['/v1/endpoint.test']['POST'], (params: unknown) => unknown>
>;
true as ExpectedFunctionSignature;
});

it('Should extract correct function signature when body is null', () => {
type APIWithNullBody = APIClass<
'/v1',
{
method: 'POST';
path: '/v1/endpoint.test';
response: {
200: ValidateFunction<unknown>;
201: ValidateFunction<unknown>;
};
body: ValidateFunction<null>;
authRequired: true;
}
>;
type ExpectedFunctionSignature = Expect<
ShallowEqual<ExtractRoutesFromAPI<APIWithNullBody>['/v1/endpoint.test']['POST'], (params: null) => unknown>
>;
true as ExpectedFunctionSignature;
});
});
Comment thread
ahmed-n-abdeltwab marked this conversation as resolved.
Outdated
2 changes: 1 addition & 1 deletion apps/meteor/app/api/server/ApiClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export type Prettify<T> = {
[K in keyof T]: T[K];
} & unknown;

type ExtractValidation<T> = T extends ValidateFunction<infer TSchema> ? TSchema : never;
type ExtractValidation<T> = T extends ValidateFunction<infer TSchema> ? TSchema : unknown;
Comment thread
ahmed-n-abdeltwab marked this conversation as resolved.
Outdated

export type ExtractRoutesFromAPI<T> =
T extends APIClass<any, infer TOperations> ? (TOperations extends MinimalRoute ? Prettify<ConvertToRoute<TOperations>> : never) : never;
Expand Down
Loading