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

refactor: Payload Generator improvements SO-232 #322

Merged
merged 14 commits into from
May 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: PayloadGenerator and put it in types
  • Loading branch information
XVincentX committed May 25, 2019
commit 77ab99fa9baa02f51d7befff37972a7aef8ff92c
17 changes: 11 additions & 6 deletions packages/http/src/mocker/HttpMocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ import { Dictionary, IHttpHeaderParam, IHttpOperation, INodeExample, INodeExtern

import * as caseless from 'caseless';
import { fromPairs, keyBy, mapValues, toPairs } from 'lodash';
import { IHttpConfig, IHttpOperationConfig, IHttpRequest, IHttpResponse, ProblemJsonError } from '../types';
import {
IHttpConfig,
IHttpOperationConfig,
IHttpRequest,
IHttpResponse,
PayloadGenerator,
ProblemJsonError,
} from '../types';
import { UNPROCESSABLE_ENTITY } from './errors';
import helpers from './negotiator/NegotiatorHelpers';
import { IHttpNegotiationResult } from './negotiator/types';

type GeneratorSignature = (f: unknown) => Promise<unknown>;

export class HttpMocker implements IMocker<IHttpOperation, IHttpRequest, IHttpConfig, IHttpResponse> {
constructor(private _exampleGenerator: GeneratorSignature) {}
constructor(private _exampleGenerator: PayloadGenerator) {}

public async mock({
resource,
Expand Down Expand Up @@ -72,7 +77,7 @@ function isINodeExample(nodeExample: INodeExample | INodeExternalExample | undef
return !!nodeExample && 'value' in nodeExample;
}

function computeMockedHeaders(headers: IHttpHeaderParam[], ex: GeneratorSignature): Promise<Dictionary<string>> {
function computeMockedHeaders(headers: IHttpHeaderParam[], ex: PayloadGenerator): Promise<Dictionary<string>> {
const headerWithPromiseValues = mapValues(keyBy(headers, h => h.name), async header => {
if (header.content) {
if (header.content.examples.length > 0) {
Expand All @@ -93,7 +98,7 @@ function computeMockedHeaders(headers: IHttpHeaderParam[], ex: GeneratorSignatur

async function computeBody(
negotiationResult: Pick<IHttpNegotiationResult, 'schema' | 'mediaType' | 'bodyExample'>,
ex: GeneratorSignature,
ex: PayloadGenerator,
) {
if (isINodeExample(negotiationResult.bodyExample) && negotiationResult.bodyExample.value !== undefined) {
return negotiationResult.bodyExample.value;
Expand Down
2 changes: 2 additions & 0 deletions packages/http/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,5 @@ export class ProblemJsonError extends Error {
Error.captureStackTrace(this, ProblemJsonError);
}
}

export type PayloadGenerator = (f: unknown) => Promise<unknown>;