@tuyau/superjson 0.0.2 → 0.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/build/client/plugin.d.ts
CHANGED
|
@@ -1,11 +1,26 @@
|
|
|
1
1
|
import { TuyauPlugin } from '@tuyau/client';
|
|
2
2
|
export * from 'superjson';
|
|
3
3
|
|
|
4
|
+
interface SuperjsonPluginConfig {
|
|
5
|
+
/**
|
|
6
|
+
* If true, the plugin will parse the response body with SuperJSON.
|
|
7
|
+
*
|
|
8
|
+
* @default true
|
|
9
|
+
*/
|
|
10
|
+
parseResponse?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* If true, the plugin will stringify the request body with SuperJSON.
|
|
13
|
+
*
|
|
14
|
+
* @default true
|
|
15
|
+
*/
|
|
16
|
+
stringifyRequest?: boolean;
|
|
17
|
+
}
|
|
4
18
|
/**
|
|
5
19
|
* Simple plugin for enabling superjson compatibility.
|
|
6
20
|
* - Set a `x-superjson` header to `true`.
|
|
7
21
|
* - Parse the response body with SuperJSON.
|
|
22
|
+
* - Stringify the request body with SuperJSON.
|
|
8
23
|
*/
|
|
9
|
-
declare function superjson(): TuyauPlugin;
|
|
24
|
+
declare function superjson(config?: SuperjsonPluginConfig): TuyauPlugin;
|
|
10
25
|
|
|
11
26
|
export { superjson };
|
package/build/client/plugin.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
// client/plugin.ts
|
|
2
2
|
import SuperJSON from "superjson";
|
|
3
3
|
export * from "superjson";
|
|
4
|
-
function superjson() {
|
|
4
|
+
function superjson(config = {}) {
|
|
5
|
+
config.stringifyRequest = config.stringifyRequest ?? true;
|
|
6
|
+
config.parseResponse = config.parseResponse ?? true;
|
|
5
7
|
return ({ options }) => {
|
|
6
8
|
options.headers = { ...options.headers, "x-superjson": "true" };
|
|
7
|
-
options.parseJson = (text) => SuperJSON.parse(text);
|
|
9
|
+
if (config.parseResponse) options.parseJson = (text) => SuperJSON.parse(text);
|
|
10
|
+
if (config.stringifyRequest) options.stringifyJson = (data) => SuperJSON.stringify(data);
|
|
8
11
|
};
|
|
9
12
|
}
|
|
10
13
|
export {
|
package/build/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// configure.ts
|
|
2
2
|
async function configure(command) {
|
|
3
3
|
const codemods = await command.createCodemods();
|
|
4
|
-
await codemods.registerMiddleware("
|
|
4
|
+
await codemods.registerMiddleware("router", [
|
|
5
5
|
{
|
|
6
6
|
path: "@tuyau/superjson/superjson_middleware",
|
|
7
7
|
position: "after"
|
|
@@ -2,7 +2,8 @@ import { HttpContext } from '@adonisjs/core/http';
|
|
|
2
2
|
import { NextFn } from '@adonisjs/core/types/http';
|
|
3
3
|
|
|
4
4
|
declare class SuperjsonMiddleware {
|
|
5
|
-
|
|
5
|
+
#private;
|
|
6
|
+
handle(ctx: HttpContext, next: NextFn): Promise<void>;
|
|
6
7
|
}
|
|
7
8
|
|
|
8
9
|
export { SuperjsonMiddleware as default };
|
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
// middleware/superjson_middleware.ts
|
|
2
2
|
import SuperJSON from "superjson";
|
|
3
3
|
var SuperjsonMiddleware = class {
|
|
4
|
-
|
|
4
|
+
#isSuperJsonRequest(ctx) {
|
|
5
|
+
return ctx.request.header("x-superjson") === "true";
|
|
6
|
+
}
|
|
7
|
+
async handle(ctx, next) {
|
|
8
|
+
const { request, response } = ctx;
|
|
9
|
+
const isSuperjsonRequest = this.#isSuperJsonRequest(ctx);
|
|
10
|
+
if (isSuperjsonRequest && request.hasBody()) {
|
|
11
|
+
request.updateBody(SuperJSON.deserialize(request.body()));
|
|
12
|
+
}
|
|
5
13
|
await next();
|
|
6
|
-
if (
|
|
7
|
-
|
|
8
|
-
if (typeof response.content !== "object") return;
|
|
9
|
-
const stringified = SuperJSON.stringify(response.getBody());
|
|
10
|
-
response.header("Content-Type", "application/json").send(stringified);
|
|
14
|
+
if (!isSuperjsonRequest || !response.hasContent || typeof response.content !== "object") return;
|
|
15
|
+
response.header("Content-Type", "application/json").send(SuperJSON.stringify(response.getBody()));
|
|
11
16
|
}
|
|
12
17
|
};
|
|
13
18
|
export {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tuyau/superjson",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.1.1",
|
|
5
5
|
"description": "Superjson transformer for Tuyau",
|
|
6
6
|
"author": "Julien Ripouteau <julien@ripouteau.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -23,13 +23,13 @@
|
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"superjson": "^2.2.2",
|
|
26
|
-
"@tuyau/utils": "0.0.
|
|
26
|
+
"@tuyau/utils": "0.0.8"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@adonisjs/assembler": "^7.8.2",
|
|
30
|
-
"@adonisjs/core": "^6.
|
|
31
|
-
"
|
|
32
|
-
"@tuyau/client": "0.2.
|
|
30
|
+
"@adonisjs/core": "^6.18.0",
|
|
31
|
+
"nock": "^14.0.5",
|
|
32
|
+
"@tuyau/client": "0.2.9"
|
|
33
33
|
},
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public",
|