chore(rest-api): TypedAction/TypedThis types (#35834)

pull/35790/head^2
Guilherme Gazzo 9 months ago committed by GitHub
parent b7ed3098e7
commit 552ee75bf3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 14
      apps/meteor/app/api/server/api.ts
  2. 13
      apps/meteor/app/api/server/definition.ts

@ -88,7 +88,7 @@ interface IAPIDefaultFieldsToExclude {
inviteToken: number;
}
type RateLimiterOptions = {
export type RateLimiterOptions = {
numRequestsAllowed?: number;
intervalTimeInMS?: number;
};
@ -320,6 +320,16 @@ export class APIClass<
};
}
public unavailableResult<T>(msg?: T): InternalError<T, 503, 'Service unavailable'> {
return {
statusCode: 503,
body: {
success: false,
error: msg || 'Service unavailable',
},
};
}
public unauthorized<T>(msg?: T): UnauthorizedResult<T> {
return {
statusCode: 401,
@ -421,7 +431,7 @@ export class APIClass<
}: {
routes: string[];
rateLimiterOptions: RateLimiterOptions | boolean;
endpoints: string[];
endpoints: Record<string, string> | string[];
}): void {
if (typeof rateLimiterOptions !== 'object') {
throw new Meteor.Error('"rateLimiterOptions" must be an object');

@ -55,10 +55,10 @@ export type ForbiddenResult<T> = {
};
};
export type InternalError<T, StatusCode = 500> = {
export type InternalError<T, StatusCode extends ErrorStatusCodes = 500, D = 'Internal server error'> = {
statusCode: StatusCode;
body: {
error: T | 'Internal server error';
error: T | D;
success: false;
};
};
@ -311,7 +311,8 @@ type Results<TResponse extends TypedOptions['response']> = {
headers?: Record<string, string>;
};
export type TypedAction<TOptions extends TypedOptions, TPath extends string = ''> = (
this: TypedThis<TOptions, TPath>,
request: Request,
) => PromiseOrValue<Results<TOptions['response']>>;
export type TypedAction<
TOptions extends TypedOptions,
TPath extends string = '',
TThis extends TypedThis<TOptions, TPath> = TypedThis<TOptions, TPath>,
> = (this: TThis, request: Request) => PromiseOrValue<Results<TOptions['response']>>;

Loading…
Cancel
Save