@contactlab/appy
Advanced tools
Comparing version 5.1.0 to 5.2.0
@@ -43,2 +43,3 @@ /** | ||
const toStringWhenJSON = (body) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
if (Object.getPrototypeOf(body).constructor.name !== 'Object') { | ||
@@ -45,0 +46,0 @@ return E.right(body); // type assertion mandatory... |
@@ -24,3 +24,3 @@ /** | ||
*/ | ||
export const withDecoder = (decoder) => (req) => pipe(req, withHeaders({ Accept: 'application/json' }), RTE.chain(resp => RTE.fromEither(pipe(parseResponse(resp), E.chain(decoder), E.bimap((e) => toResponseError(e, cloneResponse(resp.response, resp.data)), data => (Object.assign(Object.assign({}, resp), { data }))))))); | ||
export const withDecoder = (decoder) => (req) => pipe(req, withHeaders({ Accept: 'application/json' }), RTE.chain(resp => RTE.fromEither(pipe(parseResponse(resp), E.chain(decoder), E.bimap((e) => toResponseError(e, cloneResponse(resp.response, resp.data), resp.input), data => (Object.assign(Object.assign({}, resp), { data }))))))); | ||
/** | ||
@@ -27,0 +27,0 @@ * Converts a `GenericDecoder<L, A>` into a `Decoder<A>`. |
@@ -53,8 +53,9 @@ /** | ||
if (!response.ok) { | ||
return E.left(toResponseError(new Error(`Request responded with status code ${response.status}`), response)); | ||
return E.left(toResponseError(new Error(`Request responded with status code ${response.status}`), response, reqInput)); | ||
} | ||
const data = yield response[type](); | ||
return E.right({ response, data }); | ||
const data = (yield response[type]()); | ||
const result = { response, data, input: reqInput }; | ||
return E.right(result); | ||
})) | ||
.catch(e => E.left(toRequestError(e, reqInput))); | ||
.catch(e => E.left(toRequestError(E.toError(e), reqInput))); | ||
}; | ||
@@ -99,3 +100,3 @@ /** | ||
*/ | ||
export const toResponseError = (error, response) => ({ type: 'ResponseError', response, error }); | ||
export const toResponseError = (error, response, input) => ({ type: 'ResponseError', response, error, input }); | ||
/** | ||
@@ -102,0 +103,0 @@ * Normalizes the input of a `Req` to a `RequestInfoInit` tuple even when only a single `RequestInfo` is provided. |
# Changelog | ||
## [5.2.0](https://github.com/contactlab/appy/releases/tag/5.2.0) | ||
**New Feature:** | ||
- Forward `RequestInfoInit` to `ResponseError` (#743) | ||
## [5.1.0](https://github.com/contactlab/appy/releases/tag/5.1.0) | ||
@@ -4,0 +10,0 @@ |
@@ -84,2 +84,3 @@ "use strict"; | ||
var toStringWhenJSON = function (body) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
if (Object.getPrototypeOf(body).constructor.name !== 'Object') { | ||
@@ -86,0 +87,0 @@ return E.right(body); // type assertion mandatory... |
@@ -65,3 +65,3 @@ "use strict"; | ||
return RTE.fromEither((0, function_1.pipe)(parseResponse(resp), E.chain(decoder), E.bimap(function (e) { | ||
return (0, request_1.toResponseError)(e, (0, response_1.cloneResponse)(resp.response, resp.data)); | ||
return (0, request_1.toResponseError)(e, (0, response_1.cloneResponse)(resp.response, resp.data), resp.input); | ||
}, function (data) { return (__assign(__assign({}, resp), { data: data })); }))); | ||
@@ -68,0 +68,0 @@ })); |
@@ -1,1 +0,1 @@ | ||
{"name":"@contactlab/appy","version":"5.1.0","description":"A functional wrapper around Fetch API","main":"./index.js","module":"./_es6/index.js","typings":"./index.d.ts","sideEffects":false,"author":"Contactlab","license":"Apache-2.0","homepage":"https://contactlab.github.io/appy","bugs":"https://github.com/contactlab/appy/issues","repository":"contactlab/appy","keywords":["contactlab","appy","fetch","fp","fp-ts","typescript"],"engines":{"node":">= 16.0","npm":">= 8.0"},"peerDependencies":{"fp-ts":"^2.8.0"},"devDependencies":{"@types/fetch-mock":"^7.3.3","@types/glob":"^8.0.0","@types/jest":"^27.0.1","@types/node":"^16.11.12","abort-controller":"^3.0.0","cross-fetch":"^3.0.6","docs-ts":"^0.7.0","eslint":"^8.4.1","eslint-config-contactlab":"^9.0.0","eslint-config-prettier":"^8.0.0","fetch-mock":"^9.11.0","fp-ts":"^2.8.0","glob":"^8.0.1","husky":"^8.0.0","io-ts":"^2.2.13","jest":"^27.1.0","prettier":"^2.2.1","pretty-quick":"^3.1.0","ts-jest":"^27.0.5","ts-node":"^10.0.0","typescript":"^4.1.2"}} | ||
{"name":"@contactlab/appy","version":"5.2.0","description":"A functional wrapper around Fetch API","main":"./index.js","module":"./_es6/index.js","typings":"./index.d.ts","sideEffects":false,"author":"Contactlab","license":"Apache-2.0","homepage":"https://contactlab.github.io/appy","bugs":"https://github.com/contactlab/appy/issues","repository":"contactlab/appy","keywords":["contactlab","appy","fetch","fp","fp-ts","typescript"],"engines":{"node":">= 16.0","npm":">= 8.0"},"peerDependencies":{"fp-ts":"^2.8.0"},"devDependencies":{"@types/fetch-mock":"^7.3.3","@types/jest":"^29.5.3","@types/node":"^20.5.7","abort-controller":"^3.0.0","cross-fetch":"^4.0.0","docs-ts":"^0.8.0","eslint":"^8.4.1","eslint-config-contactlab":"^11.1.0","eslint-config-prettier":"^9.0.0","fetch-mock":"^9.11.0","fp-ts":"^2.8.0","husky":"^8.0.0","io-ts":"^2.2.13","jest":"^29.6.1","jest-environment-jsdom":"^29.6.1","lint-staged":"^15.0.1","prettier":"^2.2.1","ts-jest":"^29.1.1","ts-node":"^10.0.0","typescript":"^5.1.6"},"lint-staged":{"*":"prettier --ignore-unknown --write"}} |
@@ -53,3 +53,3 @@ # @contactlab/appy | ||
`Resp<A>` is an object that carries the original `Response` from a `fetch()` call and the actual retrieved `data` (of type `A`). | ||
`Resp<A>` is an object that carries the original `Response` from a `fetch()` call, the actual retrieved `data` (of type `A`) and the request's input (optional). | ||
@@ -60,2 +60,3 @@ ```ts | ||
data: A; | ||
input?: RequestInfoInit; | ||
} | ||
@@ -80,3 +81,3 @@ ``` | ||
`ResponseError` represents a response error. It carries the generated `Error` and the original `Response` object. | ||
`ResponseError` represents a response error. It carries the generated `Error`, the original `Response` object and the request's input (optional). | ||
@@ -88,2 +89,3 @@ ```ts | ||
response: Response; | ||
input?: RequestInfoInit; | ||
} | ||
@@ -184,3 +186,3 @@ ``` | ||
export const fromIots = <A>(d: D.Decoder<unknown, A>): Decoder<A> => | ||
toDecoder(d.decode, e => new Error(D.draw(e))) | ||
toDecoder(d.decode, e => new Error(D.draw(e))); | ||
``` | ||
@@ -187,0 +189,0 @@ |
@@ -47,3 +47,3 @@ /** | ||
/** | ||
* `Resp<A>` is an object that carries the original `Response` from a `fetch()` call and the actual retrieved `data` (of type `A`). | ||
* `Resp<A>` is an object that carries the original `Response` from a `fetch()` call, the actual retrieved `data` (of type `A`) and the request's input (optional). | ||
* | ||
@@ -56,2 +56,3 @@ * @category Response | ||
data: A; | ||
input?: RequestInfoInit; | ||
} | ||
@@ -77,3 +78,3 @@ /** | ||
/** | ||
* `ResponseError` represents a response error. It carriess the generated `Error` and the original `Response` object. | ||
* `ResponseError` represents a response error. It carriess the generated `Error`, the original `Response` object and the request's input (optional). | ||
* | ||
@@ -87,6 +88,7 @@ * @category Error | ||
response: Response; | ||
input?: RequestInfoInit; | ||
} | ||
type BodyTypeKey = { | ||
[K in keyof Response]-?: Response[K] extends () => Promise<unknown> ? K : never; | ||
}[keyof Response] & string; | ||
}[keyof Response]; | ||
type BodyTypeData<K extends BodyTypeKey> = ReturnType<Response[K]> extends Promise<infer _A> ? _A : never; | ||
@@ -156,3 +158,3 @@ /** | ||
*/ | ||
export declare const toResponseError: (error: Error, response: Response) => ResponseError; | ||
export declare const toResponseError: (error: Error, response: Response, input?: RequestInfoInit) => ResponseError; | ||
/** | ||
@@ -159,0 +161,0 @@ * Normalizes the input of a `Req` to a `RequestInfoInit` tuple even when only a single `RequestInfo` is provided. |
@@ -106,3 +106,3 @@ "use strict"; | ||
return fetch.apply(void 0, reqInput).then(function (response) { return __awaiter(void 0, void 0, void 0, function () { | ||
var data; | ||
var data, result; | ||
return __generator(this, function (_a) { | ||
@@ -112,12 +112,13 @@ switch (_a.label) { | ||
if (!response.ok) { | ||
return [2 /*return*/, E.left((0, exports.toResponseError)(new Error("Request responded with status code ".concat(response.status)), response))]; | ||
return [2 /*return*/, E.left((0, exports.toResponseError)(new Error("Request responded with status code ".concat(response.status)), response, reqInput))]; | ||
} | ||
return [4 /*yield*/, response[type]()]; | ||
case 1: | ||
data = _a.sent(); | ||
return [2 /*return*/, E.right({ response: response, data: data })]; | ||
data = (_a.sent()); | ||
result = { response: response, data: data, input: reqInput }; | ||
return [2 /*return*/, E.right(result)]; | ||
} | ||
}); | ||
}); }) | ||
.catch(function (e) { return E.left((0, exports.toRequestError)(e, reqInput)); }); | ||
.catch(function (e) { return E.left((0, exports.toRequestError)(E.toError(e), reqInput)); }); | ||
}; | ||
@@ -166,3 +167,3 @@ }; | ||
*/ | ||
var toResponseError = function (error, response) { return ({ type: 'ResponseError', response: response, error: error }); }; | ||
var toResponseError = function (error, response, input) { return ({ type: 'ResponseError', response: response, error: error, input: input }); }; | ||
exports.toResponseError = toResponseError; | ||
@@ -169,0 +170,0 @@ /** |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
125022
20
1842
204