Skip to content

Commit

Permalink
Create and use BaseHandler type
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS committed Aug 11, 2023
1 parent bac5e67 commit c9bb9bb
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions lib/msw-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
type SetupWorkerApi,
} from 'msw';
import type { Server } from 'miragejs';
import type { RouteHandler, ServerConfig } from 'miragejs/server';
import type {
HandlerOptions,
RouteHandler,
ServerConfig,
} from 'miragejs/server';
import type { AnyFactories, AnyModels, AnyRegistry } from 'miragejs/-types';

type RawHandler = RouteHandler<AnyRegistry> | {};
Expand All @@ -25,6 +29,13 @@ type HTTPVerb =
| 'options'
| 'head';

type BaseHandler = (
path: string,
// TODO: infer registry
handler?: RouteHandler<AnyRegistry>,
options?: HandlerOptions
) => void;

type MirageServer = {
registerRouteHandler: (
verb: HTTPVerb,
Expand All @@ -33,15 +44,15 @@ type MirageServer = {
customizedCode?: ResponseCode,
options?: unknown
) => (request: RestRequest) => ResponseData | PromiseLike<ResponseData>;
// TODO: strengthen
get?: Function;
post?: Function;
put?: Function;
delete?: Function;
del?: Function;
patch?: Function;
head?: Function;
options?: Function;

get?: BaseHandler;
post?: BaseHandler;
put?: BaseHandler;
delete?: BaseHandler;
del?: BaseHandler;
patch?: BaseHandler;
head?: BaseHandler;
options?: BaseHandler;
};

type RouteOptions = {
Expand Down Expand Up @@ -134,19 +145,19 @@ export default class MswConfig {

mirageServer?: MirageServer;

// TODO: infer models and factories
mirageConfig?: ServerConfig<AnyModels, AnyFactories>;

handlers: RestHandler[] = [];

// TODO: strengthen
get?: Function;
post?: Function;
put?: Function;
delete?: Function;
del?: Function;
patch?: Function;
head?: Function;
options?: Function;
get?: BaseHandler;
post?: BaseHandler;
put?: BaseHandler;
delete?: BaseHandler;
del?: BaseHandler;
patch?: BaseHandler;
head?: BaseHandler;
options?: BaseHandler;

create(
server: MirageServer,
Expand Down Expand Up @@ -229,6 +240,7 @@ export default class MswConfig {
});
}

// TODO: infer models and factories
config(mirageConfig: ServerConfig<AnyModels, AnyFactories>) {
/**
Sets a string to prefix all route handler URLs with.
Expand Down

0 comments on commit c9bb9bb

Please sign in to comment.