UNPKG

672 BJavaScriptView Raw
1export const deserializerMiddleware = (options, deserializer) => (next, context) => async (args) => {
2 const { response } = await next(args);
3 try {
4 const parsed = await deserializer(response, options);
5 return {
6 response,
7 output: parsed,
8 };
9 }
10 catch (error) {
11 Object.defineProperty(error, "$response", {
12 value: response,
13 });
14 if (!('$metadata' in error)) {
15 const hint = `Deserialization error: to see the raw response, inspect the hidden field {error}.$response on this object.`;
16 error.message += "\n " + hint;
17 }
18 throw error;
19 }
20};