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

Convert to TypeScript #10

Merged
merged 12 commits into from
Aug 11, 2023
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';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type isn't actually used, but because of the way the mirage types are defined, the /server types won't be found unless something is imported from the top level, in which case the other modules are cached and used as well.

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 = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had thought about this. Really baseHandler should be defined in mirageJS proper and this implement it to ensure any changes in the handler in mirage are respected by the pretender/msw modules.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it would avoid the need to create these again for the pretender repo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's a great point. In fact, I think the handlers defined in mirage are incorrect:

    /** Handle a GET request to the given path. */
    get<Response extends AnyResponse>(
      path: string,
      handler?: RouteHandler<Registry, Response>,
      options?: HandlerOptions
    ): void;

That's only one format, but they can also be just options, or just a handler, or a number of other formats (unfortunately).

I think there's still quite a bit of cleanup / deduplication to be done, but hopefully this is at least a baby step in the right direction.

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