@schmock/angular 1.0.5 → 1.1.1
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.
- package/dist/index.d.ts +62 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +84 -10
- package/package.json +3 -1
package/dist/index.d.ts
CHANGED
|
@@ -53,4 +53,66 @@ export declare function provideSchmockInterceptor(mock: CallableMockInstance, op
|
|
|
53
53
|
useClass: new () => HttpInterceptor;
|
|
54
54
|
multi: boolean;
|
|
55
55
|
};
|
|
56
|
+
/**
|
|
57
|
+
* Helper to create a 404 Not Found response
|
|
58
|
+
* @example mock('GET /api/users/999', notFound('User not found'))
|
|
59
|
+
*/
|
|
60
|
+
export declare function notFound(message?: string | object): [number, object];
|
|
61
|
+
/**
|
|
62
|
+
* Helper to create a 400 Bad Request response
|
|
63
|
+
* @example mock('POST /api/users', badRequest('Invalid email format'))
|
|
64
|
+
*/
|
|
65
|
+
export declare function badRequest(message?: string | object): [number, object];
|
|
66
|
+
/**
|
|
67
|
+
* Helper to create a 401 Unauthorized response
|
|
68
|
+
* @example mock('GET /api/protected', unauthorized('Token expired'))
|
|
69
|
+
*/
|
|
70
|
+
export declare function unauthorized(message?: string | object): [number, object];
|
|
71
|
+
/**
|
|
72
|
+
* Helper to create a 403 Forbidden response
|
|
73
|
+
* @example mock('GET /api/admin', forbidden('Admin access required'))
|
|
74
|
+
*/
|
|
75
|
+
export declare function forbidden(message?: string | object): [number, object];
|
|
76
|
+
/**
|
|
77
|
+
* Helper to create a 500 Internal Server Error response
|
|
78
|
+
* @example mock('GET /api/broken', serverError('Database connection failed'))
|
|
79
|
+
*/
|
|
80
|
+
export declare function serverError(message?: string | object): [number, object];
|
|
81
|
+
/**
|
|
82
|
+
* Helper to create a 201 Created response
|
|
83
|
+
* @example mock('POST /api/users', created({ id: 1, name: 'John' }))
|
|
84
|
+
*/
|
|
85
|
+
export declare function created(body: object): [number, object];
|
|
86
|
+
/**
|
|
87
|
+
* Helper to create a 204 No Content response
|
|
88
|
+
* @example mock('DELETE /api/users/1', noContent())
|
|
89
|
+
*/
|
|
90
|
+
export declare function noContent(): [number, null];
|
|
91
|
+
/**
|
|
92
|
+
* Pagination options
|
|
93
|
+
*/
|
|
94
|
+
export interface PaginateOptions {
|
|
95
|
+
page?: number;
|
|
96
|
+
pageSize?: number;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Paginated response structure
|
|
100
|
+
*/
|
|
101
|
+
export interface PaginatedResponse<T> {
|
|
102
|
+
data: T[];
|
|
103
|
+
page: number;
|
|
104
|
+
pageSize: number;
|
|
105
|
+
total: number;
|
|
106
|
+
totalPages: number;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Helper to create a paginated response
|
|
110
|
+
* @example
|
|
111
|
+
* const items = [{ id: 1 }, { id: 2 }, { id: 3 }]
|
|
112
|
+
* mock('GET /api/items', ({ query }) => paginate(items, {
|
|
113
|
+
* page: parseInt(query.page || '1'),
|
|
114
|
+
* pageSize: parseInt(query.pageSize || '10')
|
|
115
|
+
* }))
|
|
116
|
+
*/
|
|
117
|
+
export declare function paginate<T>(items: T[], options?: PaginateOptions): PaginatedResponse<T>;
|
|
56
118
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,eAAe,EACf,WAAW,EACZ,MAAM,sBAAsB,CAAC;AAO9B,OAAO,KAAK,EACV,oBAAoB,EAEpB,cAAc,EACf,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,eAAe,EACf,WAAW,EACZ,MAAM,sBAAsB,CAAC;AAO9B,OAAO,KAAK,EACV,oBAAoB,EAEpB,cAAc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ED;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,OAAO,GAAE,MAAM,GAAG,MAAuB,GACxC,CAAC,MAAM,EAAE,MAAM,CAAC,CAGlB;AAED;;;GAGG;AACH,wBAAgB,SAAS,CACvB,OAAO,GAAE,MAAM,GAAG,MAAoB,GACrC,CAAC,MAAM,EAAE,MAAM,CAAC,CAGlB;AAED;;;GAGG;AACH,wBAAgB,WAAW,CACzB,OAAO,GAAE,MAAM,GAAG,MAAgC,GACjD,CAAC,MAAM,EAAE,MAAM,CAAC,CAGlB;AAED;;;GAGG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAEtD;AAED;;;GAGG;AACH,wBAAgB,SAAS,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAE1C;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB,CAAC,CAAC;IAClC,IAAI,EAAE,CAAC,EAAE,CAAC;IACV,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EACxB,KAAK,EAAE,CAAC,EAAE,EACV,OAAO,GAAE,eAAoB,GAC5B,iBAAiB,CAAC,CAAC,CAAC,CAgBtB"}
|
package/dist/index.js
CHANGED
|
@@ -35,6 +35,19 @@ function toHttpMethod(method) {
|
|
|
35
35
|
}
|
|
36
36
|
return "GET";
|
|
37
37
|
}
|
|
38
|
+
function getStatusText(status) {
|
|
39
|
+
const statusTexts = {
|
|
40
|
+
200: "OK",
|
|
41
|
+
201: "Created",
|
|
42
|
+
204: "No Content",
|
|
43
|
+
400: "Bad Request",
|
|
44
|
+
401: "Unauthorized",
|
|
45
|
+
403: "Forbidden",
|
|
46
|
+
404: "Not Found",
|
|
47
|
+
500: "Internal Server Error"
|
|
48
|
+
};
|
|
49
|
+
return statusTexts[status] || "Unknown";
|
|
50
|
+
}
|
|
38
51
|
function extractQueryParams(request) {
|
|
39
52
|
const result = {};
|
|
40
53
|
request.params.keys().forEach((key) => {
|
|
@@ -130,15 +143,26 @@ function createSchmockInterceptor(mock, options = {}) {
|
|
|
130
143
|
if (transformResponse) {
|
|
131
144
|
response = transformResponse(response, req);
|
|
132
145
|
}
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
146
|
+
const status = response.status || 200;
|
|
147
|
+
if (status >= 400) {
|
|
148
|
+
observer.error(new HttpErrorResponse({
|
|
149
|
+
error: response.body,
|
|
150
|
+
status,
|
|
151
|
+
statusText: getStatusText(status),
|
|
152
|
+
url: req.url,
|
|
153
|
+
headers: new HttpHeaders(response.headers || {})
|
|
154
|
+
}));
|
|
155
|
+
} else {
|
|
156
|
+
const httpResponse = new HttpResponse({
|
|
157
|
+
body: response.body,
|
|
158
|
+
status,
|
|
159
|
+
statusText: getStatusText(status),
|
|
160
|
+
url: req.url,
|
|
161
|
+
headers: new HttpHeaders(response.headers || {})
|
|
162
|
+
});
|
|
163
|
+
observer.next(httpResponse);
|
|
164
|
+
observer.complete();
|
|
165
|
+
}
|
|
142
166
|
}
|
|
143
167
|
}).catch((error) => {
|
|
144
168
|
let errorBody;
|
|
@@ -173,7 +197,57 @@ function provideSchmockInterceptor(mock, options) {
|
|
|
173
197
|
multi: true
|
|
174
198
|
};
|
|
175
199
|
}
|
|
200
|
+
function notFound(message = "Not Found") {
|
|
201
|
+
const body = typeof message === "string" ? { message } : message;
|
|
202
|
+
return [404, body];
|
|
203
|
+
}
|
|
204
|
+
function badRequest(message = "Bad Request") {
|
|
205
|
+
const body = typeof message === "string" ? { message } : message;
|
|
206
|
+
return [400, body];
|
|
207
|
+
}
|
|
208
|
+
function unauthorized(message = "Unauthorized") {
|
|
209
|
+
const body = typeof message === "string" ? { message } : message;
|
|
210
|
+
return [401, body];
|
|
211
|
+
}
|
|
212
|
+
function forbidden(message = "Forbidden") {
|
|
213
|
+
const body = typeof message === "string" ? { message } : message;
|
|
214
|
+
return [403, body];
|
|
215
|
+
}
|
|
216
|
+
function serverError(message = "Internal Server Error") {
|
|
217
|
+
const body = typeof message === "string" ? { message } : message;
|
|
218
|
+
return [500, body];
|
|
219
|
+
}
|
|
220
|
+
function created(body) {
|
|
221
|
+
return [201, body];
|
|
222
|
+
}
|
|
223
|
+
function noContent() {
|
|
224
|
+
return [204, null];
|
|
225
|
+
}
|
|
226
|
+
function paginate(items, options = {}) {
|
|
227
|
+
const page = options.page || 1;
|
|
228
|
+
const pageSize = options.pageSize || 10;
|
|
229
|
+
const total = items.length;
|
|
230
|
+
const totalPages = Math.ceil(total / pageSize);
|
|
231
|
+
const start = (page - 1) * pageSize;
|
|
232
|
+
const end = start + pageSize;
|
|
233
|
+
const data = items.slice(start, end);
|
|
234
|
+
return {
|
|
235
|
+
data,
|
|
236
|
+
page,
|
|
237
|
+
pageSize,
|
|
238
|
+
total,
|
|
239
|
+
totalPages
|
|
240
|
+
};
|
|
241
|
+
}
|
|
176
242
|
export {
|
|
243
|
+
unauthorized,
|
|
244
|
+
serverError,
|
|
177
245
|
provideSchmockInterceptor,
|
|
178
|
-
|
|
246
|
+
paginate,
|
|
247
|
+
notFound,
|
|
248
|
+
noContent,
|
|
249
|
+
forbidden,
|
|
250
|
+
created,
|
|
251
|
+
createSchmockInterceptor,
|
|
252
|
+
badRequest
|
|
179
253
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@schmock/angular",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Angular HTTP interceptor adapter for Schmock",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"build:types": "rm -f tsconfig.tsbuildinfo && tsc --build",
|
|
23
23
|
"test": "vitest run",
|
|
24
24
|
"test:watch": "vitest",
|
|
25
|
+
"test:bdd": "vitest run --config vitest.config.bdd.ts",
|
|
25
26
|
"lint": "biome check src",
|
|
26
27
|
"lint:fix": "biome check --write src",
|
|
27
28
|
"check:publish": "publint && attw --pack --ignore-rules cjs-resolves-to-esm"
|
|
@@ -33,6 +34,7 @@
|
|
|
33
34
|
"rxjs": "^7.0.0"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
37
|
+
"@amiceli/vitest-cucumber": "^6.1.0",
|
|
36
38
|
"@angular/common": "^21.0.3",
|
|
37
39
|
"@angular/compiler": "^20.3.9",
|
|
38
40
|
"@angular/core": "^21.0.3",
|