Skip to content

Commit

Permalink
Adds new Request Handler API. Rewrites "rest" request handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Jan 27, 2021
1 parent b15ba73 commit 5ecda68
Show file tree
Hide file tree
Showing 20 changed files with 749 additions and 494 deletions.
14 changes: 0 additions & 14 deletions src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,20 +286,6 @@ function graphQLRequestHandler<QueryType, VariablesType = Record<string, any>>(
console.log('Response:', loggedResponse)
console.groupEnd()
},

getMetaInfo() {
const header =
expectedOperationType === 'all'
? `[graphql] ${expectedOperationType} (origin: ${mask.toString()})`
: `[graphql] ${expectedOperationType} ${expectedOperationName} (origin: ${mask.toString()})`

return {
type: 'graphql',
header,
mask,
callFrame,
}
},
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as context from './context'
export { context }

export { setupWorker } from './setupWorker/setupWorker'
export { SetupWorkerApi } from './setupWorker/glossary'
Expand All @@ -12,27 +13,18 @@ export {
ResponseCompositionOptions,
ResponseFunction,
} from './response'
export { context }

/* Request handlers */
export {
defaultContext,
MockedRequest,
RequestHandler,
RequestHandlerMetaInfo,
RequestParams,
RequestQuery,
ResponseResolver,
ResponseResolverReturnType,
AsyncResponseResolverReturnType,
} from './utils/handlers/requestHandler'
export {
rest,
restContext,
RestContext,
RESTMethods,
ParsedRestRequest,
} from './rest'
export {
graphql,
graphqlContext,
Expand All @@ -47,3 +39,11 @@ export { matchRequestUrl } from './utils/matching/matchRequestUrl'
/* Utils */
export { compose } from './utils/internal/compose'
export { DelayMode } from './context/delay'

export { RequestHandler as BaseRequestHandler } from './utils/handlers/2.0/RequestHandler'
export { rest } from './rest'
export {
RESTMethods,
RestContext,
restContext,
} from './utils/handlers/2.0/RestHandler'
16 changes: 7 additions & 9 deletions src/node/createSetupServer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as cookieUtils from 'cookie'
import { bold } from 'chalk'
import * as cookieUtils from 'cookie'
import { Headers, flattenHeadersObject } from 'headers-utils'
import { StrictEventEmitter } from 'strict-event-emitter'
import {
RequestInterceptor,
MockedResponse as MockedInterceptedResponse,
Interceptor,
} from 'node-request-interceptor'
import { RequestHandlersList } from '../setupWorker/glossary'
import { RequestApplicator, RequestHandlersList } from '../setupWorker/glossary'
import { MockedRequest } from '../utils/handlers/requestHandler'
import { getResponse } from '../utils/getResponse'
import { parseBody } from '../utils/request/parseBody'
Expand All @@ -17,7 +17,6 @@ import { onUnhandledRequest } from '../utils/request/onUnhandledRequest'
import { ServerLifecycleEventsMap, SetupServerApi } from './glossary'
import { SharedOptions } from '../sharedOptions'
import { uuidv4 } from '../utils/internal/uuidv4'
import { request } from 'express'

const DEFAULT_LISTEN_OPTIONS: SharedOptions = {
onUnhandledRequest: 'bypass',
Expand All @@ -31,7 +30,7 @@ export function createSetupServer(...interceptors: Interceptor[]) {
const emitter = new StrictEventEmitter<ServerLifecycleEventsMap>()

return function setupServer(
...requestHandlers: RequestHandlersList
...requestHandlers: RequestApplicator[]
): SetupServerApi {
requestHandlers.forEach((handler) => {
if (Array.isArray(handler))
Expand All @@ -50,7 +49,7 @@ export function createSetupServer(...interceptors: Interceptor[]) {

// Store the list of request handlers for the current server instance,
// so it could be modified at a runtime.
let currentHandlers: RequestHandlersList = [...requestHandlers]
let currentHandlers: RequestApplicator[] = [...requestHandlers]

interceptor.on('response', (req, res) => {
const requestId = req.headers?.['x-msw-request-id'] as string
Expand Down Expand Up @@ -91,7 +90,6 @@ export function createSetupServer(...interceptors: Interceptor[]) {
body: parseBody(req.body, requestHeaders),
headers: requestHeaders,
cookies: {},
params: {},
redirect: 'manual',
referrer: '',
keepalive: false,
Expand Down Expand Up @@ -176,11 +174,11 @@ export function createSetupServer(...interceptors: Interceptor[]) {

printHandlers() {
currentHandlers.forEach((handler) => {
const meta = handler.getMetaInfo()
const { header, callFrame } = handler.info

console.log(`\
${bold(meta.header)}
Declaration: ${meta.callFrame}
${bold(header)}
Declaration: ${callFrame}
`)
})
},
Expand Down
6 changes: 3 additions & 3 deletions src/node/glossary.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MockedResponse as MockedInterceptedResponse } from 'node-request-interceptor'
import { SharedOptions } from '../sharedOptions'
import { RequestHandlersList } from '../setupWorker/glossary'
import { RequestApplicator } from '../setupWorker/glossary'
import { MockedRequest } from '../utils/handlers/requestHandler'

export interface ServerLifecycleEventsMap {
Expand Down Expand Up @@ -29,7 +29,7 @@ export interface SetupServerApi {
* Prepends given request handlers to the list of existing handlers.
* @see {@link https://mswjs.io/docs/api/setup-server/use `server.use()`}
*/
use(...handlers: RequestHandlersList): void
use(...handlers: RequestApplicator[]): void

/**
* Marks all request handlers that respond using `res.once()` as unused.
Expand All @@ -41,7 +41,7 @@ export interface SetupServerApi {
* Resets request handlers to the initial list given to the `setupServer` call, or to the explicit next request handlers list, if given.
* @see {@link https://mswjs.io/docs/api/setup-server/reset-handlers `server.reset-handlers()`}
*/
resetHandlers(...nextHandlers: RequestHandlersList): void
resetHandlers(...nextHandlers: RequestApplicator[]): void

/**
* Lists all active request handlers.
Expand Down
Loading

0 comments on commit 5ecda68

Please sign in to comment.