@typespec/spec-api 0.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/LICENSE +21 -0
  3. package/dist/expectation.d.ts +63 -0
  4. package/dist/expectation.d.ts.map +1 -0
  5. package/dist/expectation.js +87 -0
  6. package/dist/expectation.js.map +1 -0
  7. package/dist/index.d.ts +8 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +8 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/mock-request.d.ts +22 -0
  12. package/dist/mock-request.d.ts.map +1 -0
  13. package/dist/mock-request.js +23 -0
  14. package/dist/mock-request.js.map +1 -0
  15. package/dist/request-validations.d.ts +24 -0
  16. package/dist/request-validations.d.ts.map +1 -0
  17. package/dist/request-validations.js +164 -0
  18. package/dist/request-validations.js.map +1 -0
  19. package/dist/response-utils.d.ts +15 -0
  20. package/dist/response-utils.d.ts.map +1 -0
  21. package/dist/response-utils.js +24 -0
  22. package/dist/response-utils.js.map +1 -0
  23. package/dist/routes.d.ts +65 -0
  24. package/dist/routes.d.ts.map +1 -0
  25. package/dist/routes.js +79 -0
  26. package/dist/routes.js.map +1 -0
  27. package/dist/scenarios.d.ts +26 -0
  28. package/dist/scenarios.d.ts.map +1 -0
  29. package/dist/scenarios.js +50 -0
  30. package/dist/scenarios.js.map +1 -0
  31. package/dist/types.d.ts +96 -0
  32. package/dist/types.d.ts.map +1 -0
  33. package/dist/types.js +2 -0
  34. package/dist/types.js.map +1 -0
  35. package/dist/validation-error.d.ts +14 -0
  36. package/dist/validation-error.d.ts.map +1 -0
  37. package/dist/validation-error.js +20 -0
  38. package/dist/validation-error.js.map +1 -0
  39. package/package.json +54 -0
  40. package/src/expectation.ts +106 -0
  41. package/src/index.ts +48 -0
  42. package/src/mock-request.ts +31 -0
  43. package/src/request-validations.ts +216 -0
  44. package/src/response-utils.ts +26 -0
  45. package/src/routes.ts +92 -0
  46. package/src/scenarios.ts +76 -0
  47. package/src/types.ts +125 -0
  48. package/src/validation-error.ts +21 -0
  49. package/temp/.tsbuildinfo +1 -0
  50. package/test/expectation.test.ts +53 -0
  51. package/tsconfig.build.json +8 -0
  52. package/tsconfig.json +10 -0
  53. package/vitest.config.ts +11 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ # @typespec/spec-api
2
+
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Microsoft Corporation. All rights reserved.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE
@@ -0,0 +1,63 @@
1
+ import { CollectionFormat, RequestExt } from "./types.js";
2
+ /**
3
+ * Class containing all the expectations that can be run on the request.
4
+ */
5
+ export declare class RequestExpectation {
6
+ private originalRequest;
7
+ constructor(originalRequest: RequestExt);
8
+ /**
9
+ * Expect the raw body of the request to match the given string.
10
+ * @param rawBody Raw request body.
11
+ * @throws {ValidationError} if there is an error.
12
+ */
13
+ rawBodyEquals(expectedRawBody: string | Buffer | undefined): void;
14
+ /**
15
+ * Expect the body of the request to match the given object.
16
+ * @param rawBody Raw request body.
17
+ * @throws {ValidationError} if there is an error.
18
+ */
19
+ bodyEquals(expectedRawBody: unknown | undefined): void;
20
+ /**
21
+ * Expect the coerced body of the request to match the given object.
22
+ * @param rawBody Raw request body.
23
+ * @throws {ValidationError} if there is an error.
24
+ */
25
+ coercedBodyEquals(expectedRawBody: unknown | undefined): void;
26
+ /**
27
+ * Expect the body of the request to be empty.
28
+ * @throws {ValidationError} if there is an error.
29
+ */
30
+ bodyEmpty(): void;
31
+ /**
32
+ * Expect the body of the request to be not empty.
33
+ * @throws {ValidationError} if there is an error.
34
+ */
35
+ bodyNotEmpty(): void;
36
+ /**
37
+ * Expect the header of the request contains the expected key/value pair
38
+ * @param headerName Key expected in header
39
+ * @param expectedValue Values expected in header
40
+ * @throws {ValidationError} if there is an error.
41
+ */
42
+ containsHeader(headerName: string, expectedValue: string): void;
43
+ /**
44
+ * Expect the query string of the request contains the expected name/value pair.
45
+ * @param paramName Name of the query parameter.
46
+ * @param expectedValue Value expected of the query parameter.
47
+ */
48
+ containsQueryParam(paramName: string, expectedValue: string | string[], collectionFormat?: CollectionFormat): void;
49
+ /**
50
+ * Check if two requests are equal
51
+ * @param actual Actual value
52
+ * @param expected Expected value
53
+ */
54
+ deepEqual(actual: unknown, expected: unknown, message?: string): void;
55
+ /**
56
+ * Expect the body of the request to be semantically equivalent to the provided XML string.
57
+ * The XML declaration prefix will automatically be added to expectedBody.
58
+ * @param expectedBody expected value of request body.
59
+ * @throws {ValidationError} if there is an error.
60
+ */
61
+ xmlBodyEquals(expectedBody: string): void;
62
+ }
63
+ //# sourceMappingURL=expectation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"expectation.d.ts","sourceRoot":"","sources":["../src/expectation.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAG1D;;GAEG;AACH,qBAAa,kBAAkB;IACV,OAAO,CAAC,eAAe;gBAAf,eAAe,EAAE,UAAU;IACtD;;;;OAIG;IACI,aAAa,CAAC,eAAe,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,IAAI;IAIxE;;;;OAIG;IACI,UAAU,CAAC,eAAe,EAAE,OAAO,GAAG,SAAS,GAAG,IAAI;IAI7D;;;;OAIG;IACI,iBAAiB,CAAC,eAAe,EAAE,OAAO,GAAG,SAAS,GAAG,IAAI;IAIpE;;;OAGG;IACI,SAAS,IAAI,IAAI;IAIxB;;;OAGG;IACI,YAAY,IAAI,IAAI;IAI3B;;;;;OAKG;IACI,cAAc,CAAC,UAAU,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,IAAI;IAItE;;;;OAIG;IACI,kBAAkB,CACvB,SAAS,EAAE,MAAM,EACjB,aAAa,EAAE,MAAM,GAAG,MAAM,EAAE,EAChC,gBAAgB,CAAC,EAAE,gBAAgB,GAClC,IAAI;IAIP;;;;OAIG;IACI,SAAS,CAAC,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,SAA0B,GAAG,IAAI;IAM7F;;;;;OAKG;IACI,aAAa,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI;CAGjD"}
@@ -0,0 +1,87 @@
1
+ import deepEqual from "deep-equal";
2
+ import { validateBodyEmpty, validateBodyEquals, validateBodyNotEmpty, validateCoercedDateBodyEquals, validateHeader, validateQueryParam, validateRawBodyEquals, validateXmlBodyEquals, } from "./request-validations.js";
3
+ import { ValidationError } from "./validation-error.js";
4
+ /**
5
+ * Class containing all the expectations that can be run on the request.
6
+ */
7
+ export class RequestExpectation {
8
+ originalRequest;
9
+ constructor(originalRequest) {
10
+ this.originalRequest = originalRequest;
11
+ }
12
+ /**
13
+ * Expect the raw body of the request to match the given string.
14
+ * @param rawBody Raw request body.
15
+ * @throws {ValidationError} if there is an error.
16
+ */
17
+ rawBodyEquals(expectedRawBody) {
18
+ validateRawBodyEquals(this.originalRequest, expectedRawBody);
19
+ }
20
+ /**
21
+ * Expect the body of the request to match the given object.
22
+ * @param rawBody Raw request body.
23
+ * @throws {ValidationError} if there is an error.
24
+ */
25
+ bodyEquals(expectedRawBody) {
26
+ validateBodyEquals(this.originalRequest, expectedRawBody);
27
+ }
28
+ /**
29
+ * Expect the coerced body of the request to match the given object.
30
+ * @param rawBody Raw request body.
31
+ * @throws {ValidationError} if there is an error.
32
+ */
33
+ coercedBodyEquals(expectedRawBody) {
34
+ validateCoercedDateBodyEquals(this.originalRequest, expectedRawBody);
35
+ }
36
+ /**
37
+ * Expect the body of the request to be empty.
38
+ * @throws {ValidationError} if there is an error.
39
+ */
40
+ bodyEmpty() {
41
+ validateBodyEmpty(this.originalRequest);
42
+ }
43
+ /**
44
+ * Expect the body of the request to be not empty.
45
+ * @throws {ValidationError} if there is an error.
46
+ */
47
+ bodyNotEmpty() {
48
+ validateBodyNotEmpty(this.originalRequest);
49
+ }
50
+ /**
51
+ * Expect the header of the request contains the expected key/value pair
52
+ * @param headerName Key expected in header
53
+ * @param expectedValue Values expected in header
54
+ * @throws {ValidationError} if there is an error.
55
+ */
56
+ containsHeader(headerName, expectedValue) {
57
+ validateHeader(this.originalRequest, headerName, expectedValue);
58
+ }
59
+ /**
60
+ * Expect the query string of the request contains the expected name/value pair.
61
+ * @param paramName Name of the query parameter.
62
+ * @param expectedValue Value expected of the query parameter.
63
+ */
64
+ containsQueryParam(paramName, expectedValue, collectionFormat) {
65
+ validateQueryParam(this.originalRequest, paramName, expectedValue, collectionFormat);
66
+ }
67
+ /**
68
+ * Check if two requests are equal
69
+ * @param actual Actual value
70
+ * @param expected Expected value
71
+ */
72
+ deepEqual(actual, expected, message = "Values not deep equal") {
73
+ if (!deepEqual(actual, expected, { strict: true })) {
74
+ throw new ValidationError(message, expected, actual);
75
+ }
76
+ }
77
+ /**
78
+ * Expect the body of the request to be semantically equivalent to the provided XML string.
79
+ * The XML declaration prefix will automatically be added to expectedBody.
80
+ * @param expectedBody expected value of request body.
81
+ * @throws {ValidationError} if there is an error.
82
+ */
83
+ xmlBodyEquals(expectedBody) {
84
+ validateXmlBodyEquals(this.originalRequest, expectedBody);
85
+ }
86
+ }
87
+ //# sourceMappingURL=expectation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"expectation.js","sourceRoot":"","sources":["../src/expectation.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,oBAAoB,EACpB,6BAA6B,EAC7B,cAAc,EACd,kBAAkB,EAClB,qBAAqB,EACrB,qBAAqB,GACtB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD;;GAEG;AACH,MAAM,OAAO,kBAAkB;IACF;IAA3B,YAA2B,eAA2B;QAA3B,oBAAe,GAAf,eAAe,CAAY;IAAG,CAAC;IAC1D;;;;OAIG;IACI,aAAa,CAAC,eAA4C;QAC/D,qBAAqB,CAAC,IAAI,CAAC,eAAe,EAAE,eAAe,CAAC,CAAC;IAC/D,CAAC;IAED;;;;OAIG;IACI,UAAU,CAAC,eAAoC;QACpD,kBAAkB,CAAC,IAAI,CAAC,eAAe,EAAE,eAAe,CAAC,CAAC;IAC5D,CAAC;IAED;;;;OAIG;IACI,iBAAiB,CAAC,eAAoC;QAC3D,6BAA6B,CAAC,IAAI,CAAC,eAAe,EAAE,eAAe,CAAC,CAAC;IACvE,CAAC;IAED;;;OAGG;IACI,SAAS;QACd,iBAAiB,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;IAC1C,CAAC;IAED;;;OAGG;IACI,YAAY;QACjB,oBAAoB,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;IAC7C,CAAC;IAED;;;;;OAKG;IACI,cAAc,CAAC,UAAkB,EAAE,aAAqB;QAC7D,cAAc,CAAC,IAAI,CAAC,eAAe,EAAE,UAAU,EAAE,aAAa,CAAC,CAAC;IAClE,CAAC;IAED;;;;OAIG;IACI,kBAAkB,CACvB,SAAiB,EACjB,aAAgC,EAChC,gBAAmC;QAEnC,kBAAkB,CAAC,IAAI,CAAC,eAAe,EAAE,SAAS,EAAE,aAAa,EAAE,gBAAgB,CAAC,CAAC;IACvF,CAAC;IAED;;;;OAIG;IACI,SAAS,CAAC,MAAe,EAAE,QAAiB,EAAE,OAAO,GAAG,uBAAuB;QACpF,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;YACnD,MAAM,IAAI,eAAe,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;QACvD,CAAC;IACH,CAAC;IAED;;;;;OAKG;IACI,aAAa,CAAC,YAAoB;QACvC,qBAAqB,CAAC,IAAI,CAAC,eAAe,EAAE,YAAY,CAAC,CAAC;IAC5D,CAAC;CACF"}
@@ -0,0 +1,8 @@
1
+ export { MockRequest } from "./mock-request.js";
2
+ export { BODY_EMPTY_ERROR_MESSAGE, BODY_NOT_EMPTY_ERROR_MESSAGE, BODY_NOT_EQUAL_ERROR_MESSAGE, validateBodyEmpty, validateBodyEquals, validateBodyNotEmpty, validateCoercedDateBodyEquals, validateHeader, validateQueryParam, validateRawBodyEquals, validateValueFormat, validateXmlBodyEquals, } from "./request-validations.js";
3
+ export { json, xml } from "./response-utils.js";
4
+ export { mockapi } from "./routes.js";
5
+ export { WithKeysScenarioExpect, passOnCode, passOnSuccess, withKeys, withServiceKeys, } from "./scenarios.js";
6
+ export { CollectionFormat, Fail, HttpMethod, KeyedMockApi, KeyedMockRequestHandler, KeyedMockResponse, MockApi, MockApiDefinition, MockApiForHandler, MockRequestHandler, MockResponse, MockResponseBody, PassByKeyScenario, PassByServiceKeyScenario, PassOnCodeScenario, PassOnSuccessScenario, RequestExt, ScenarioMockApi, ScenarioPassCondition, ServiceRequestFile, SimpleMockRequestHandler, } from "./types.js";
7
+ export { ValidationError } from "./validation-error.js";
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EACL,wBAAwB,EACxB,4BAA4B,EAC5B,4BAA4B,EAC5B,iBAAiB,EACjB,kBAAkB,EAClB,oBAAoB,EACpB,6BAA6B,EAC7B,cAAc,EACd,kBAAkB,EAClB,qBAAqB,EACrB,mBAAmB,EACnB,qBAAqB,GACtB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EACL,sBAAsB,EACtB,UAAU,EACV,aAAa,EACb,QAAQ,EACR,eAAe,GAChB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,gBAAgB,EAChB,IAAI,EACJ,UAAU,EACV,YAAY,EACZ,uBAAuB,EACvB,iBAAiB,EACjB,OAAO,EACP,iBAAiB,EACjB,iBAAiB,EACjB,kBAAkB,EAClB,YAAY,EACZ,gBAAgB,EAChB,iBAAiB,EACjB,wBAAwB,EACxB,kBAAkB,EAClB,qBAAqB,EACrB,UAAU,EACV,eAAe,EACf,qBAAqB,EACrB,kBAAkB,EAClB,wBAAwB,GACzB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export { MockRequest } from "./mock-request.js";
2
+ export { BODY_EMPTY_ERROR_MESSAGE, BODY_NOT_EMPTY_ERROR_MESSAGE, BODY_NOT_EQUAL_ERROR_MESSAGE, validateBodyEmpty, validateBodyEquals, validateBodyNotEmpty, validateCoercedDateBodyEquals, validateHeader, validateQueryParam, validateRawBodyEquals, validateValueFormat, validateXmlBodyEquals, } from "./request-validations.js";
3
+ export { json, xml } from "./response-utils.js";
4
+ export { mockapi } from "./routes.js";
5
+ export { passOnCode, passOnSuccess, withKeys, withServiceKeys, } from "./scenarios.js";
6
+ export { Fail, } from "./types.js";
7
+ export { ValidationError } from "./validation-error.js";
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EACL,wBAAwB,EACxB,4BAA4B,EAC5B,4BAA4B,EAC5B,iBAAiB,EACjB,kBAAkB,EAClB,oBAAoB,EACpB,6BAA6B,EAC7B,cAAc,EACd,kBAAkB,EAClB,qBAAqB,EACrB,mBAAmB,EACnB,qBAAqB,GACtB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAEL,UAAU,EACV,aAAa,EACb,QAAQ,EACR,eAAe,GAChB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAEL,IAAI,GAoBL,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC"}
@@ -0,0 +1,22 @@
1
+ import { RequestExpectation } from "./expectation.js";
2
+ import { RequestExt } from "./types.js";
3
+ export declare class MockRequest {
4
+ originalRequest: RequestExt;
5
+ readonly expect: RequestExpectation;
6
+ readonly baseUrl: string;
7
+ readonly headers: {
8
+ [key: string]: string;
9
+ };
10
+ readonly query: {
11
+ [key: string]: string | string[];
12
+ };
13
+ readonly params: {
14
+ [key: string]: string;
15
+ };
16
+ readonly body: any;
17
+ readonly files?: {
18
+ [fieldname: string]: Express.Multer.File[];
19
+ } | Express.Multer.File[] | undefined;
20
+ constructor(originalRequest: RequestExt);
21
+ }
22
+ //# sourceMappingURL=mock-request.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock-request.d.ts","sourceRoot":"","sources":["../src/mock-request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAExC,qBAAa,WAAW;IAeI,eAAe,EAAE,UAAU;IAdrD,SAAgB,MAAM,EAAE,kBAAkB,CAAC;IAE3C,SAAgB,OAAO,EAAE,MAAM,CAAC;IAChC,SAAgB,OAAO,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;IACnD,SAAgB,KAAK,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAA;KAAE,CAAC;IAC5D,SAAgB,MAAM,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;IAClD,SAAgB,IAAI,EAAE,GAAG,CAAC;IAC1B,SAAgB,KAAK,CAAC,EAClB;QACE,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;KAC5C,GACD,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,GACrB,SAAS,CAAC;gBAEY,eAAe,EAAE,UAAU;CAStD"}
@@ -0,0 +1,23 @@
1
+ import { RequestExpectation } from "./expectation.js";
2
+ export class MockRequest {
3
+ originalRequest;
4
+ expect;
5
+ baseUrl;
6
+ headers;
7
+ query;
8
+ params;
9
+ body;
10
+ files;
11
+ constructor(originalRequest) {
12
+ this.originalRequest = originalRequest;
13
+ this.baseUrl = getRequestBaseUrl(originalRequest);
14
+ this.expect = new RequestExpectation(originalRequest);
15
+ this.headers = originalRequest.headers;
16
+ this.query = originalRequest.query;
17
+ this.params = originalRequest.params;
18
+ this.body = originalRequest.body;
19
+ this.files = originalRequest.files;
20
+ }
21
+ }
22
+ const getRequestBaseUrl = (request) => `${request.protocol}://${request.get("host")}`;
23
+ //# sourceMappingURL=mock-request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock-request.js","sourceRoot":"","sources":["../src/mock-request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAGtD,MAAM,OAAO,WAAW;IAeI;IAdV,MAAM,CAAqB;IAE3B,OAAO,CAAS;IAChB,OAAO,CAA4B;IACnC,KAAK,CAAuC;IAC5C,MAAM,CAA4B;IAClC,IAAI,CAAM;IACV,KAAK,CAKP;IAEd,YAA0B,eAA2B;QAA3B,oBAAe,GAAf,eAAe,CAAY;QACnD,IAAI,CAAC,OAAO,GAAG,iBAAiB,CAAC,eAAe,CAAC,CAAC;QAClD,IAAI,CAAC,MAAM,GAAG,IAAI,kBAAkB,CAAC,eAAe,CAAC,CAAC;QACtD,IAAI,CAAC,OAAO,GAAG,eAAe,CAAC,OAAoC,CAAC;QACpE,IAAI,CAAC,KAAK,GAAG,eAAe,CAAC,KAAkC,CAAC;QAChE,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC,MAAmC,CAAC;QAClE,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC,IAAI,CAAC;QACjC,IAAI,CAAC,KAAK,GAAG,eAAe,CAAC,KAAK,CAAC;IACrC,CAAC;CACF;AAED,MAAM,iBAAiB,GAAG,CAAC,OAAmB,EAAU,EAAE,CACxD,GAAG,OAAO,CAAC,QAAQ,MAAM,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC"}
@@ -0,0 +1,24 @@
1
+ import { CollectionFormat, RequestExt } from "./types.js";
2
+ export declare const BODY_NOT_EQUAL_ERROR_MESSAGE = "Body provided doesn't match expected body";
3
+ export declare const BODY_EMPTY_ERROR_MESSAGE = "Body should exists";
4
+ export declare const BODY_NOT_EMPTY_ERROR_MESSAGE = "Body should be empty";
5
+ export declare const validateRawBodyEquals: (request: RequestExt, expectedRawBody: string | Buffer | undefined) => void;
6
+ export declare const validateBodyEquals: (request: RequestExt, expectedBody: unknown | undefined) => void;
7
+ export declare const validateXmlBodyEquals: (request: RequestExt, expectedBody: string) => void;
8
+ export declare const validateCoercedDateBodyEquals: (request: RequestExt, expectedBody: unknown | undefined) => void;
9
+ export declare const validateBodyEmpty: (request: RequestExt) => void;
10
+ export declare const validateBodyNotEmpty: (request: RequestExt) => void;
11
+ /**
12
+ * Check whether the request header contains the given name/value pair
13
+ */
14
+ export declare const validateHeader: (request: RequestExt, headerName: string, expected: string) => void;
15
+ /**
16
+ * Check whether the query string contains the given parameter name and value.
17
+ * Supports query param as string or collection. e.g. if it's a collection, one can call the method like this: validateQueryParam(request, ["a", "b", "c"], "multi")
18
+ */
19
+ export declare const validateQueryParam: (request: RequestExt, paramName: string, expected: string | string[], collectionFormat?: CollectionFormat) => void;
20
+ /**
21
+ * Check whether the value follow the right format.
22
+ */
23
+ export declare const validateValueFormat: (value: string, format: "uuid" | "rfc7231" | "rfc3339") => void;
24
+ //# sourceMappingURL=request-validations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-validations.d.ts","sourceRoot":"","sources":["../src/request-validations.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAG1D,eAAO,MAAM,4BAA4B,8CAA8C,CAAC;AACxF,eAAO,MAAM,wBAAwB,uBAAuB,CAAC;AAC7D,eAAO,MAAM,4BAA4B,yBAAyB,CAAC;AAEnE,eAAO,MAAM,qBAAqB,YACvB,UAAU,mBACF,MAAM,GAAG,MAAM,GAAG,SAAS,KAC3C,IAaF,CAAC;AAEF,eAAO,MAAM,kBAAkB,YACpB,UAAU,gBACL,OAAO,GAAG,SAAS,KAChC,IAWF,CAAC;AAEF,eAAO,MAAM,qBAAqB,YAAa,UAAU,gBAAgB,MAAM,KAAG,IA8BjF,CAAC;AAEF,eAAO,MAAM,6BAA6B,YAC/B,UAAU,gBACL,OAAO,GAAG,SAAS,KAChC,IAWF,CAAC;AAEF,eAAO,MAAM,iBAAiB,YAAa,UAAU,KAAG,IAUvD,CAAC;AAEF,eAAO,MAAM,oBAAoB,YAAa,UAAU,KAAG,IAU1D,CAAC;AAUF;;GAEG;AACH,eAAO,MAAM,cAAc,YAAa,UAAU,cAAc,MAAM,YAAY,MAAM,KAAG,IAK1F,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,kBAAkB,YACpB,UAAU,aACR,MAAM,YACP,MAAM,GAAG,MAAM,EAAE,qBACR,gBAAgB,KAClC,IA+BF,CAAC;AAWF;;GAEG;AACH,eAAO,MAAM,mBAAmB,UACvB,MAAM,UACL,MAAM,GAAG,SAAS,GAAG,SAAS,KACrC,IAwBF,CAAC"}
@@ -0,0 +1,164 @@
1
+ import deepEqual from "deep-equal";
2
+ import * as prettier from "prettier";
3
+ import { parseString } from "xml2js";
4
+ import { ValidationError } from "./validation-error.js";
5
+ export const BODY_NOT_EQUAL_ERROR_MESSAGE = "Body provided doesn't match expected body";
6
+ export const BODY_EMPTY_ERROR_MESSAGE = "Body should exists";
7
+ export const BODY_NOT_EMPTY_ERROR_MESSAGE = "Body should be empty";
8
+ export const validateRawBodyEquals = (request, expectedRawBody) => {
9
+ const actualRawBody = request.rawBody;
10
+ if (expectedRawBody == null) {
11
+ if (!isBodyEmpty(actualRawBody)) {
12
+ throw new ValidationError(BODY_NOT_EQUAL_ERROR_MESSAGE, expectedRawBody, actualRawBody);
13
+ }
14
+ return;
15
+ }
16
+ if (!deepEqual(actualRawBody, expectedRawBody, { strict: true })) {
17
+ throw new ValidationError(BODY_NOT_EQUAL_ERROR_MESSAGE, expectedRawBody, actualRawBody);
18
+ }
19
+ };
20
+ export const validateBodyEquals = (request, expectedBody) => {
21
+ if (expectedBody == null) {
22
+ if (!isBodyEmpty(request.rawBody)) {
23
+ throw new ValidationError(BODY_NOT_EQUAL_ERROR_MESSAGE, expectedBody, request.rawBody);
24
+ }
25
+ return;
26
+ }
27
+ if (!deepEqual(request.body, expectedBody, { strict: true })) {
28
+ throw new ValidationError(BODY_NOT_EQUAL_ERROR_MESSAGE, expectedBody, request.body);
29
+ }
30
+ };
31
+ export const validateXmlBodyEquals = (request, expectedBody) => {
32
+ if (request.rawBody === undefined || isBodyEmpty(request.rawBody)) {
33
+ throw new ValidationError(BODY_EMPTY_ERROR_MESSAGE, expectedBody, request.rawBody);
34
+ }
35
+ expectedBody = `<?xml version='1.0' encoding='UTF-8'?>` + expectedBody;
36
+ let actualParsedBody = "";
37
+ parseString(request.rawBody, (err, result) => {
38
+ if (err !== null) {
39
+ throw err;
40
+ }
41
+ actualParsedBody = result;
42
+ });
43
+ let expectedParsedBody = "";
44
+ parseString(expectedBody, (err, result) => {
45
+ if (err !== null) {
46
+ throw err;
47
+ }
48
+ expectedParsedBody = result;
49
+ });
50
+ if (!deepEqual(actualParsedBody, expectedParsedBody, { strict: true })) {
51
+ throw new ValidationError(BODY_NOT_EQUAL_ERROR_MESSAGE, prettier.format(expectedBody), prettier.format(request.body));
52
+ }
53
+ };
54
+ export const validateCoercedDateBodyEquals = (request, expectedBody) => {
55
+ if (expectedBody == null) {
56
+ if (!isBodyEmpty(request.rawBody)) {
57
+ throw new ValidationError(BODY_NOT_EQUAL_ERROR_MESSAGE, expectedBody, request.rawBody);
58
+ }
59
+ return;
60
+ }
61
+ if (!deepEqual(coerceDate(request.body), expectedBody, { strict: true })) {
62
+ throw new ValidationError(BODY_NOT_EQUAL_ERROR_MESSAGE, expectedBody, request.body);
63
+ }
64
+ };
65
+ export const validateBodyEmpty = (request) => {
66
+ if (isBodyEmpty(request.rawBody)) {
67
+ if (request.body instanceof Buffer) {
68
+ if (request.body.length > 0) {
69
+ throw new ValidationError(BODY_NOT_EMPTY_ERROR_MESSAGE, undefined, request.rawBody);
70
+ }
71
+ }
72
+ }
73
+ else {
74
+ throw new ValidationError(BODY_EMPTY_ERROR_MESSAGE, undefined, request.rawBody);
75
+ }
76
+ };
77
+ export const validateBodyNotEmpty = (request) => {
78
+ if (isBodyEmpty(request.rawBody)) {
79
+ if (request.body instanceof Buffer) {
80
+ if (request.body.length === 0) {
81
+ throw new ValidationError(BODY_EMPTY_ERROR_MESSAGE, undefined, request.rawBody);
82
+ }
83
+ }
84
+ else {
85
+ throw new ValidationError(BODY_EMPTY_ERROR_MESSAGE, undefined, request.rawBody);
86
+ }
87
+ }
88
+ };
89
+ /**
90
+ * Check if the provided body is empty.
91
+ * @param body express.js request body.
92
+ */
93
+ const isBodyEmpty = (body) => {
94
+ return body == null || body === "" || body.length === 0;
95
+ };
96
+ /**
97
+ * Check whether the request header contains the given name/value pair
98
+ */
99
+ export const validateHeader = (request, headerName, expected) => {
100
+ const actual = request.headers[headerName];
101
+ if (actual !== expected) {
102
+ throw new ValidationError(`Expected ${expected} but got ${actual}`, expected, actual);
103
+ }
104
+ };
105
+ /**
106
+ * Check whether the query string contains the given parameter name and value.
107
+ * Supports query param as string or collection. e.g. if it's a collection, one can call the method like this: validateQueryParam(request, ["a", "b", "c"], "multi")
108
+ */
109
+ export const validateQueryParam = (request, paramName, expected, collectionFormat) => {
110
+ const actual = request.query[paramName];
111
+ const splitterMap = {
112
+ csv: ",",
113
+ ssv: " ",
114
+ tsv: "\t",
115
+ pipes: "|",
116
+ };
117
+ let isExpected = false;
118
+ if (collectionFormat && Array.isArray(expected)) {
119
+ // verify query parameter as collection
120
+ if (collectionFormat === "multi" && Array.isArray(actual)) {
121
+ isExpected = deepEqual(actual, expected);
122
+ }
123
+ else if (collectionFormat !== "multi" && typeof actual === "string") {
124
+ const expectedString = expected.join(splitterMap[collectionFormat]);
125
+ isExpected = expectedString === decodeURIComponent(actual);
126
+ }
127
+ if (!isExpected) {
128
+ throw new ValidationError(`Expected query param collection ${paramName}=${expected} in ${collectionFormat}, but got ${actual}`, expected, actual);
129
+ }
130
+ }
131
+ else if (actual !== expected) {
132
+ throw new ValidationError(`Expected query param ${paramName}=${expected} but got ${actual}`, expected, actual);
133
+ }
134
+ };
135
+ const coerceDate = (targetObject) => {
136
+ let stringRep = JSON.stringify(targetObject);
137
+ stringRep = stringRep.replace(/(\d\d\d\d-\d\d-\d\d[Tt]\d\d:\d\d:\d\d)(\.\d{3,7})?([Zz]|[+-]00:00)/g, "$1Z");
138
+ return JSON.parse(stringRep);
139
+ };
140
+ /**
141
+ * Check whether the value follow the right format.
142
+ */
143
+ export const validateValueFormat = (value, format) => {
144
+ switch (format) {
145
+ case "uuid":
146
+ if (!/^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$/i.test(value)) {
147
+ throw new ValidationError(`Expected uuid format but got ${value}`, "uuid", value);
148
+ }
149
+ break;
150
+ case "rfc7231":
151
+ if (!/^(Mon|Tue|Wed|Thu|Fri|Sat|Sun),\s\d{2}\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{4}\s\d{2}:\d{2}:\d{2}\sGMT$/i.test(value)) {
152
+ throw new ValidationError(`Expected rfc7231 format but got ${value}`, "rfc7231", value);
153
+ }
154
+ break;
155
+ case "rfc3339":
156
+ if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})?$/i.test(value)) {
157
+ throw new ValidationError(`Expected rfc3339 format but got ${value}`, "rfc3339", value);
158
+ }
159
+ break;
160
+ default:
161
+ throw new ValidationError(`Unsupported format ${format}`, format, value);
162
+ }
163
+ };
164
+ //# sourceMappingURL=request-validations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-validations.js","sourceRoot":"","sources":["../src/request-validations.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,KAAK,QAAQ,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAErC,OA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,EAAE,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;YACpF,CAAC;YACD,MAAM;QACR,KAAK,SAAS;YACZ,IACE,CAAC,4HAA4H,CAAC,IAAI,CAChI,KAAK,CACN,EACD,CAAC;gBACD,MAAM,IAAI,eAAe,CAAC,mCAAmC,KAAK,EAAE,EAAE,SAAS,EAAE,KAAK,CAAC,CAAC;YAC1F,CAAC;YACD,MAAM;QACR,KAAK,SAAS;YACZ,IAAI,CAAC,oEAAoE,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;gBACtF,MAAM,IAAI,eAAe,CAAC,mCAAmC,KAAK,EAAE,EAAE,SAAS,EAAE,KAAK,CAAC,CAAC;YAC1F,CAAC;YACD,MAAM;QACR;YACE,MAAM,IAAI,eAAe,CAAC,sBAAsB,MAAM,EAAE,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;IAC7E,CAAC;AACH,CAAC,CAAC"}
@@ -0,0 +1,15 @@
1
+ import { MockResponseBody } from "./types.js";
2
+ /**
3
+ * Serialize the provided content as json to use in a MockResponse body.
4
+ * @content Object to return as json.
5
+ * @returns {MockResponseBody} response body with application/json content type.
6
+ */
7
+ export declare function json(content: unknown): MockResponseBody;
8
+ /**
9
+ * Sends the provided XML string in a MockResponse body.
10
+ * The XML declaration prefix will automatically be added to xmlString.
11
+ * @content Object to return as XML.
12
+ * @returns {MockResponseBody} response body with application/xml content type.
13
+ */
14
+ export declare function xml(xmlString: string): MockResponseBody;
15
+ //# sourceMappingURL=response-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"response-utils.d.ts","sourceRoot":"","sources":["../src/response-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;GAIG;AACH,wBAAgB,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,gBAAgB,CAKvD;AAED;;;;;GAKG;AACH,wBAAgB,GAAG,CAAC,SAAS,EAAE,MAAM,GAAG,gBAAgB,CAKvD"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Serialize the provided content as json to use in a MockResponse body.
3
+ * @content Object to return as json.
4
+ * @returns {MockResponseBody} response body with application/json content type.
5
+ */
6
+ export function json(content) {
7
+ return {
8
+ contentType: "application/json",
9
+ rawContent: JSON.stringify(content),
10
+ };
11
+ }
12
+ /**
13
+ * Sends the provided XML string in a MockResponse body.
14
+ * The XML declaration prefix will automatically be added to xmlString.
15
+ * @content Object to return as XML.
16
+ * @returns {MockResponseBody} response body with application/xml content type.
17
+ */
18
+ export function xml(xmlString) {
19
+ return {
20
+ contentType: "application/xml",
21
+ rawContent: `<?xml version='1.0' encoding='UTF-8'?>` + xmlString,
22
+ };
23
+ }
24
+ //# sourceMappingURL=response-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"response-utils.js","sourceRoot":"","sources":["../src/response-utils.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,UAAU,IAAI,CAAC,OAAgB;IACnC,OAAO;QACL,WAAW,EAAE,kBAAkB;QAC/B,UAAU,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;KACpC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,GAAG,CAAC,SAAiB;IACnC,OAAO;QACL,WAAW,EAAE,iBAAiB;QAC9B,UAAU,EAAE,wCAAwC,GAAG,SAAS;KACjE,CAAC;AACJ,CAAC"}
@@ -0,0 +1,65 @@
1
+ import { HttpMethod, MockApiForHandler, MockRequestHandler } from "./types.js";
2
+ /**
3
+ * Register a GET request for the provided uri.
4
+ * @param uri URI to match.
5
+ * @param func Request handler.
6
+ */
7
+ declare function get<const T extends MockRequestHandler>(uri: string, func: T): MockApiForHandler<T>;
8
+ /**
9
+ * Register a POST request for the provided uri.
10
+ * @param uri URI to match.
11
+ * @param func Request handler.
12
+ */
13
+ declare function post<T extends MockRequestHandler>(uri: string, func: T): MockApiForHandler<T>;
14
+ /**
15
+ * Register a PUT request for the provided uri.
16
+ * @param uri URI to match.
17
+ * @param func Request handler.
18
+ */
19
+ declare function put<T extends MockRequestHandler>(uri: string, func: T): MockApiForHandler<T>;
20
+ /**
21
+ * Register a PATCH request for the provided uri.
22
+ * @param uri URI to match.
23
+ * @param func Request handler.
24
+ */
25
+ declare function patch<T extends MockRequestHandler>(uri: string, func: T): MockApiForHandler<T>;
26
+ /**
27
+ * Register a DELETE request for the provided uri.
28
+ * @param uri URI to match.
29
+ * @param func Request handler.
30
+ */
31
+ declare function deleteReq<T extends MockRequestHandler>(uri: string, func: T): MockApiForHandler<T>;
32
+ /**
33
+ * Register a Options request for the provided uri.
34
+ * @param uri URI to match.
35
+ * @param func Request handler.
36
+ */
37
+ declare function options<T extends MockRequestHandler>(uri: string, func: T): MockApiForHandler<T>;
38
+ /**
39
+ * Register a HEAD request for the provided uri.
40
+ * @param uri URI to match.
41
+ * @param name Name of the scenario(For coverage).
42
+ * @param func Request handler.
43
+ */
44
+ declare function head<T extends MockRequestHandler>(uri: string, func: T): MockApiForHandler<T>;
45
+ /**
46
+ * Register a request for the provided uri.
47
+ * @param method Method to use.
48
+ * @param uri URI to match.
49
+ * @param func Request handler.
50
+ *
51
+ * @note prefer to use the corresponding method method directly instead of `request()`(i.e `get(), post()`)
52
+ */
53
+ declare function request<T extends MockRequestHandler>(method: HttpMethod, uri: string, handler: T): MockApiForHandler<T>;
54
+ export declare const mockapi: {
55
+ get: typeof get;
56
+ post: typeof post;
57
+ put: typeof put;
58
+ patch: typeof patch;
59
+ delete: typeof deleteReq;
60
+ options: typeof options;
61
+ head: typeof head;
62
+ request: typeof request;
63
+ };
64
+ export {};
65
+ //# sourceMappingURL=routes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAE/E;;;;GAIG;AAC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