Skip to content

Commit

Permalink
Fixed jest versioning issue
Browse files Browse the repository at this point in the history
  • Loading branch information
justsml committed Dec 1, 2021
1 parent 2252eb9 commit 16b5759
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 1,160 deletions.
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const config: Config.InitialOptions = {
"<rootDir>/server",
"<rootDir>/packages/api-validator"
],
verbose: true,
};

export default config;
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@
]
},
"devDependencies": {
"jest": "^27.3.1",
"ts-jest": "^27.0.7",
"ts-node": "^10.4.0"
"jest": "^26.6.0",
"ts-jest": "^26.5.0",
"ts-node": "^10.4.0",
"typescript": "^4.5.2"

}
}
16 changes: 15 additions & 1 deletion packages/api-validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@ export type TransformFunction<TInput, TOutput> = (
options?: any
) => TOutput;
/** PathPrefix is a url path prefix (e.g. `/posts/latest`) */
export type PathRule<TInput, TOutput> =
export type Rules<TInput, TOutput> =
| TransformFunction<TInput, TOutput>
| ValidateFunction<TInput>
| {
response?: TransformFunction<TInput, TOutput> | ValidateFunction<TInput>;
request?: TransformFunction<TInput, TOutput> | ValidateFunction<TInput>;
// requestBody: TransformFunction<TInput, TOutput>;
// requestHeaders: TransformFunction<TInput, TOutput>;
// requestUrl: TransformFunction<TInput, TOutput>;
};

/** pathPattern is a key expression in the following format:
*
* ```
* {
* `/users/:id?`: userSchemaCheck, // matches `/users/123` and `/users/`
* `POST:/messages`: { request: messageSchemaCheck }, // matches `/messages`
* }
* ```
*/
export type HttpPathRules<TInput, TOutput> = {
[pathPattern: string]: Rules<TInput, TOutput>;
};
3 changes: 3 additions & 0 deletions packages/api-validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
"devDependencies": {
"@types/node-fetch": "v2",
"zod": "^3.11.6"
},
"dependencies": {
"path-to-regexp": "^6.2.0"
}
}
57 changes: 48 additions & 9 deletions packages/api-validator/src/validateFetchFactory.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
/* eslint-disable @typescript-eslint/no-redeclare */
import type { RequestInit, Response } from "node-fetch";
import { pathToRegexp } from "path-to-regexp";
import { HttpPathRules, Rules } from "..";

import { PathRule } from "..";
type RuleMatcher<TInput, TOutput> = (
path: string,
method: string
) => Rules<TInput, TOutput> | false;

function getPathMatcher<TInput, TOutput>(
rules: HttpPathRules<TInput, TOutput>
): RuleMatcher<TInput, TOutput> {
const paths = Object.keys(rules);

const pathMatchers = paths.map((p) => ({
path: p, // TODO: Add HTTP VERB Support here-ish
pattern: pathToRegexp(p),
}));

return (inputPath: string, method: string) => {
const matchingPath = pathMatchers.find(({ pattern }) =>
pattern.test(inputPath)
);
const { path } = matchingPath || {};
return path !== undefined ? rules[path] : false;
};
}
export default function fetchFactory<TInput, TOutput>(
validator: PathRule<TInput, TOutput>
validator: Rules<TInput, TOutput> | HttpPathRules<TInput, TOutput> | false
) {
const requestBodyValidator =
typeof validator === "object" ? validator.request : undefined;
const responseValidator =
typeof validator === "function" ? validator : validator.response;

let pathMatcher: RuleMatcher<TInput, TOutput> | null = null;
if (
typeof validator === "object" &&
!validator.request &&
!validator.response
) {
// We have some path patterns to match!
pathMatcher = getPathMatcher(validator);
}
return function fetchWrapper(url: string, init: RequestInit = {}) {
let _response: Response;
if (pathMatcher !== null) {
validator = pathMatcher(url, init.method || "GET");
}
let requestBodyValidator =
typeof validator === "object" ? validator.request : undefined;
let responseValidator =
typeof validator === "function"
? validator
: typeof validator === "object"
? validator.response
: undefined;
// check request body
if (requestBodyValidator) {
if (requestBodyValidator && typeof requestBodyValidator === "function") {
// Currently only JSON is supported.
if (typeof init.body === "string") {
try {
Expand All @@ -40,7 +79,7 @@ export default function fetchFactory<TInput, TOutput>(
})
.then((body: TOutput) => {
// validate response
if (responseValidator) {
if (responseValidator && typeof responseValidator === "function") {
const validatorResult = responseValidator(body as any);
if (!validatorResult)
throw TypeError(`Invalid response body: ${JSON.stringify(body)}`);
Expand Down
5 changes: 5 additions & 0 deletions packages/api-validator/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ mime-types@^2.1.12:
dependencies:
mime-db "1.51.0"

path-to-regexp@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-6.2.0.tgz#f7b3803336104c346889adece614669230645f38"
integrity sha512-f66KywYG6+43afgE/8j/GoiNyygk/bnoCbps++3ErRKsIYkGGupyv07R2Ok5m9i67Iqc+T2g1eAUGUPzWhYTyg==

zod@^3.11.6:
version "3.11.6"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.11.6.tgz#e43a5e0c213ae2e02aefe7cb2b1a6fa3d7f1f483"
Expand Down
Loading

0 comments on commit 16b5759

Please sign in to comment.