@mockmaster/core 1.0.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.
@@ -0,0 +1,4 @@
1
+ export * from './types';
2
+ export { matchPath, parsePathParams } from './match';
3
+ export { matchMethod, findMatchingMock } from './mock';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,SAAS,CAAA;AAGvB,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAGpD,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ // Core mocking engine
2
+ // This will be the main entry point for the core package
3
+ // Type definitions
4
+ export * from './types';
5
+ // Path matching utilities
6
+ export { matchPath, parsePathParams } from './match';
7
+ // Mock management utilities
8
+ export { matchMethod, findMatchingMock } from './mock';
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sBAAsB;AACtB,yDAAyD;AAEzD,mBAAmB;AACnB,cAAc,SAAS,CAAA;AAEvB,0BAA0B;AAC1B,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAEpD,4BAA4B;AAC5B,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAA"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Matches a path against a pattern
3
+ * @param pattern - The pattern to match against (e.g., '/users', '/users/:id', '/api/*')
4
+ * @param path - The actual request path
5
+ * @returns true if the path matches the pattern
6
+ */
7
+ export declare const matchPath: (pattern: string, path: string) => boolean;
8
+ /**
9
+ * Extracts path parameters from a path based on a pattern
10
+ * @param pattern - The pattern with :param placeholders
11
+ * @param path - The actual request path
12
+ * @returns Record of parameter names to values
13
+ */
14
+ export declare const parsePathParams: (pattern: string, path: string) => Record<string, string>;
15
+ //# sourceMappingURL=match.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"match.d.ts","sourceRoot":"","sources":["../src/match.ts"],"names":[],"mappings":"AAmCA;;;;;GAKG;AACH,eAAO,MAAM,SAAS,GAAI,SAAS,MAAM,EAAE,MAAM,MAAM,KAAG,OAazD,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,eAAe,GAAI,SAAS,MAAM,EAAE,MAAM,MAAM,KAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAcpF,CAAA"}
package/dist/match.js ADDED
@@ -0,0 +1,63 @@
1
+ // Pure functions for request matching
2
+ /**
3
+ * Converts a path pattern to a regular expression
4
+ * @param pattern - The pattern with potential :params and wildcards
5
+ * @returns RegExp that matches the pattern
6
+ */
7
+ const patternToRegex = (pattern) => {
8
+ // First, replace wildcards with a placeholder to protect them from escaping
9
+ const withWildcardPlaceholder = pattern.replace(/\*/g, '__WILDCARD__');
10
+ // Escape special regex characters except for :param
11
+ const escaped = withWildcardPlaceholder.replace(/[.+?^${}()|[\]\\]/g, '\\$&');
12
+ // Replace :param with named capture group that matches any non-slash characters
13
+ const withParams = escaped.replace(/:(\w+)/g, '(?<$1>[^/]+)');
14
+ // Replace wildcard placeholder
15
+ // Check if wildcard is at the end - if so, match everything
16
+ // Otherwise, match only non-slash characters (single path segment)
17
+ const withWildcards = withParams.replace(/__WILDCARD__/g, (match, offset, string) => {
18
+ // If wildcard is at the end, match everything including slashes
19
+ if (offset + match.length >= string.length || string[offset + match.length] === '$') {
20
+ return '.*';
21
+ }
22
+ // Otherwise, match only within a single path segment
23
+ return '[^/]+';
24
+ });
25
+ return new RegExp(`^${withWildcards}$`);
26
+ };
27
+ /**
28
+ * Matches a path against a pattern
29
+ * @param pattern - The pattern to match against (e.g., '/users', '/users/:id', '/api/*')
30
+ * @param path - The actual request path
31
+ * @returns true if the path matches the pattern
32
+ */
33
+ export const matchPath = (pattern, path) => {
34
+ // Quick check for exact match (optimization)
35
+ if (pattern === path) {
36
+ return true;
37
+ }
38
+ // Check if pattern has parameters or wildcards
39
+ if (pattern.includes(':') || pattern.includes('*')) {
40
+ const regex = patternToRegex(pattern);
41
+ return regex.test(path);
42
+ }
43
+ return false;
44
+ };
45
+ /**
46
+ * Extracts path parameters from a path based on a pattern
47
+ * @param pattern - The pattern with :param placeholders
48
+ * @param path - The actual request path
49
+ * @returns Record of parameter names to values
50
+ */
51
+ export const parsePathParams = (pattern, path) => {
52
+ // If no parameters in pattern, return empty object
53
+ if (!pattern.includes(':')) {
54
+ return {};
55
+ }
56
+ const regex = patternToRegex(pattern);
57
+ const match = path.match(regex);
58
+ if (!match || !match.groups) {
59
+ return {};
60
+ }
61
+ return match.groups;
62
+ };
63
+ //# sourceMappingURL=match.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"match.js","sourceRoot":"","sources":["../src/match.ts"],"names":[],"mappings":"AAAA,sCAAsC;AAEtC;;;;GAIG;AACH,MAAM,cAAc,GAAG,CAAC,OAAe,EAAU,EAAE;IACjD,4EAA4E;IAC5E,MAAM,uBAAuB,GAAG,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,cAAc,CAAC,CAAA;IAEtE,oDAAoD;IACpD,MAAM,OAAO,GAAG,uBAAuB,CAAC,OAAO,CAAC,oBAAoB,EAAE,MAAM,CAAC,CAAA;IAE7E,gFAAgF;IAChF,MAAM,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,cAAc,CAAC,CAAA;IAE7D,+BAA+B;IAC/B,4DAA4D;IAC5D,mEAAmE;IACnE,MAAM,aAAa,GAAG,UAAU,CAAC,OAAO,CACtC,eAAe,EACf,CAAC,KAAa,EAAE,MAAc,EAAE,MAAc,EAAE,EAAE;QAChD,gEAAgE;QAChE,IAAI,MAAM,GAAG,KAAK,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM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
1
+ import type { Mock, MockRequest, HttpMethod } from './types';
2
+ /**
3
+ * Matches an HTTP method (case-insensitive)
4
+ * @param expected - The expected HTTP method
5
+ * @param actual - The actual HTTP method from the request
6
+ * @returns true if methods match
7
+ */
8
+ export declare const matchMethod: (expected: HttpMethod, actual: HttpMethod) => boolean;
9
+ /**
10
+ * Finds the first mock that matches the given request
11
+ * @param mocks - Array of mocks to search through
12
+ * @param request - The request to match against
13
+ * @returns The first matching mock, or undefined if none match
14
+ */
15
+ export declare const findMatchingMock: (mocks: Mock[], request: MockRequest) => Mock | undefined;
16
+ //# sourceMappingURL=mock.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock.d.ts","sourceRoot":"","sources":["../src/mock.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAE5D;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,UAAU,UAAU,EAAE,QAAQ,UAAU,KAAG,OAEtE,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,GAAI,OAAO,IAAI,EAAE,EAAE,SAAS,WAAW,KAAG,IAAI,GAAG,SAY7E,CAAA"}
package/dist/mock.js ADDED
@@ -0,0 +1,30 @@
1
+ // Pure functions for mock management
2
+ import { matchPath } from './match';
3
+ /**
4
+ * Matches an HTTP method (case-insensitive)
5
+ * @param expected - The expected HTTP method
6
+ * @param actual - The actual HTTP method from the request
7
+ * @returns true if methods match
8
+ */
9
+ export const matchMethod = (expected, actual) => {
10
+ return expected.toUpperCase() === actual.toUpperCase();
11
+ };
12
+ /**
13
+ * Finds the first mock that matches the given request
14
+ * @param mocks - Array of mocks to search through
15
+ * @param request - The request to match against
16
+ * @returns The first matching mock, or undefined if none match
17
+ */
18
+ export const findMatchingMock = (mocks, request) => {
19
+ return mocks.find((mock) => {
20
+ // Check if method matches
21
+ const methodMatches = matchMethod(mock.method, request.method);
22
+ if (!methodMatches) {
23
+ return false;
24
+ }
25
+ // Check if path matches
26
+ const pathMatches = matchPath(mock.pattern, request.path);
27
+ return pathMatches;
28
+ });
29
+ };
30
+ //# sourceMappingURL=mock.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock.js","sourceRoot":"","sources":["../src/mock.ts"],"names":[],"mappings":"AAAA,qCAAqC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAGnC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,QAAoB,EAAE,MAAkB,EAAW,EAAE;IAC/E,OAAO,QAAQ,CAAC,WAAW,EAAE,KAAK,MAAM,CAAC,WAAW,EAAE,CAAA;AACxD,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,KAAa,EAAE,OAAoB,EAAoB,EAAE;IACxF,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE;QACzB,0BAA0B;QAC1B,MAAM,aAAa,GAAG,WAAW,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAA;QAC9D,IAAI,CAAC,aAAa,EAAE,CAAC;YACnB,OAAO,KAAK,CAAA;QACd,CAAC;QAED,wBAAwB;QACxB,MAAM,WAAW,GAAG,SAAS,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;QACzD,OAAO,WAAW,CAAA;IACpB,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA"}
@@ -0,0 +1,21 @@
1
+ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
2
+ export interface MockRequest {
3
+ method: HttpMethod;
4
+ path: string;
5
+ query?: Record<string, string>;
6
+ headers?: Record<string, string>;
7
+ body?: unknown;
8
+ }
9
+ export interface MockResponse {
10
+ status: number;
11
+ headers?: Record<string, string>;
12
+ body?: unknown;
13
+ delay?: number;
14
+ }
15
+ export interface Mock {
16
+ id: string;
17
+ pattern: string;
18
+ method: HttpMethod;
19
+ response: MockResponse | ((request: MockRequest) => MockResponse);
20
+ }
21
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,SAAS,CAAA;AAEzF,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,UAAU,CAAA;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC9B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAChC,IAAI,CAAC,EAAE,OAAO,CAAA;CACf;AAED,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAChC,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,IAAI;IACnB,EAAE,EAAE,MAAM,CAAA;IACV,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,UAAU,CAAA;IAClB,QAAQ,EAAE,YAAY,GAAG,CAAC,CAAC,OAAO,EAAE,WAAW,KAAK,YAAY,CAAC,CAAA;CAClE"}
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ // Core types for mock-master
2
+ export {};
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,6BAA6B"}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@mockmaster/core",
3
+ "version": "1.0.0",
4
+ "description": "Core mocking engine for mock-master",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "keywords": [
18
+ "mock",
19
+ "testing",
20
+ "api"
21
+ ],
22
+ "license": "MIT",
23
+ "dependencies": {
24
+ "fp-ts": "^2.16.2"
25
+ },
26
+ "devDependencies": {
27
+ "@types/node": "^20.11.0",
28
+ "@vitest/coverage-v8": "^1.2.0",
29
+ "typescript": "^5.3.3",
30
+ "vitest": "^1.2.0"
31
+ },
32
+ "scripts": {
33
+ "build": "tsc",
34
+ "dev": "tsc --watch",
35
+ "test": "vitest run",
36
+ "test:watch": "vitest",
37
+ "test:coverage": "vitest run --coverage",
38
+ "lint": "eslint src --ext .ts",
39
+ "typecheck": "tsc --noEmit",
40
+ "clean": "rm -rf dist"
41
+ }
42
+ }