Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"copyreadme": "cp README.md packages/bolt-core/README.md && cp README.md packages/bolt-plexus/README.md && cp README.md packages/bolt-fastify/README.md"
},
"devDependencies": {
"@plexusjs/napi": "^1.9.2",
"@plexusjs/napi": "^1.10.3",
"@types/node": "^18.11.11",
"fastify": "^4.21.0",
"lerna": "7.1.1",
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion packages/bolt-core/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions packages/bolt-fastify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,28 @@ export class BoltServer<T extends RouterRecord> {
}
},
handler: this.handlers.get(handlerKey(route)) as any,
onSend: async (request, reply, payload) => {
if (typeof payload === 'string') {
console.log('got payload', payload.slice(0, 100))
const pickKey = Object.keys(request.headers).find((header) => header.toLowerCase() === 'x-bolt-pick');
const pickHeader = request.headers[pickKey ?? 'x-bolt-pick'];
// JSON !
// If we have a pick header, pick the fields
if (pickHeader) {
console.log('got pick header', pickHeader)
try {
const picking = JSON.parse(pickHeader as string);
if (Array.isArray(picking)) {
const data = JSON.parse(payload) as any;
return JSON.stringify(picking.reduce((acc, field) => ({ ...acc, [field]: data[field] }), {}));
}
} catch (error) {
console.error('failed to parse payload', error)
}
}
}
return payload;
}
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/bolt-fastify/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/bolt-plexus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"@bolt-ts/core": "0.0.7"
},
"peerDependencies": {
"@plexusjs/napi": "^1.0.0"
"@plexusjs/napi": "^1.10.3"
}
}
18 changes: 10 additions & 8 deletions packages/bolt-plexus/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RouteBuilder, RouteParams, RouteSettings, RouterRecord, UnsetMarker, getParseFn } from '@bolt-ts/core';

import { ApiInstance, PlexusApiRes } from '@plexusjs/napi';
import { ApiInstance, PlexusApiFetchOptions, PlexusApiRes, PlexusApiSendOptions } from '@plexusjs/napi';

type StringParamToObject<P extends string> = P extends `${infer _Prefix}:${infer Param}/${infer Rest}`
? { [K in Param]: string | number } & StringParamToObject<Rest>
Expand Down Expand Up @@ -52,7 +52,9 @@ export type IOutput<TParams extends RouteParams> = TParams['_output_out'] extend
? undefined
: TParams['_output_out'];

export type IMappedOptions<TParams extends RouteParams> = IParams<TParams> & IQuery<TParams> & IBody<TParams>;
export type IMappedOptions<TParams extends RouteParams> = IParams<TParams> & IQuery<TParams> & IBody<TParams> & {
headers?: PlexusApiFetchOptions['headers']
};

export type PlexusMappedRoute<TParams extends RouteParams> = (
params: IMappedOptions<TParams>
Expand Down Expand Up @@ -92,7 +94,7 @@ export function boltRouteToPlexus<TParams extends RouteParams>(
route: RouteBuilder<TParams>,
options?: RouteSettings
): PlexusMappedRoute<TParams> {
return async ({ params, query, body }) => {
return async ({ params, query, body, headers }) => {
const method = `${route._def.method ?? 'get'}`?.toLowerCase() as ['get', 'post', 'put', 'patch', 'delete'][number];

// Build path
Expand Down Expand Up @@ -167,15 +169,15 @@ export function boltRouteToPlexus<TParams extends RouteParams>(

switch (method) {
case 'get':
return api.get<IOutput<TParams>>(path, query ?? {});
return api.get<IOutput<TParams>>(path, query ?? {}, { headers });
case 'post':
return api.post<IOutput<TParams>>(path, body ?? {});
return api.post<IOutput<TParams>>(path, body ?? {}, { headers });
case 'put':
return api.put<IOutput<TParams>>(path, body ?? {});
return api.put<IOutput<TParams>>(path, body ?? {}, { headers });
case 'patch':
return api.patch<IOutput<TParams>>(path, body ?? {});
return api.patch<IOutput<TParams>>(path, body ?? {}, { headers });
case 'delete':
return api.delete<IOutput<TParams>>(path);
return api.delete<IOutput<TParams>>(path, {}, { headers });
}
};
}
16 changes: 16 additions & 0 deletions packages/bolt-plexus/src/pick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { RouteParams } from '@bolt-ts/core';
import { IOutput, PlexusMappedRoute } from '.';

// todo: add support for array picking

export function pick <T extends RouteParams, F = Array<keyof IOutput<T>>> (route: PlexusMappedRoute<T>, fields: F): PlexusMappedRoute<T> {
return async (params: Parameters<PlexusMappedRoute<T>>[0]) => {
return route({
...params,
headers: {
...params.headers,
'x-bolt-pick': JSON.stringify(fields)
}
});
}
}
2 changes: 1 addition & 1 deletion packages/bolt-plexus/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -438,25 +438,25 @@
resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33"
integrity sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==

"@plexusjs/api@1.9.2":
version "1.9.2"
resolved "https://registry.yarnpkg.com/@plexusjs/api/-/api-1.9.2.tgz#3d1da9006ca43924cfcddcca1ea22d32831910fa"
integrity sha512-nguTncGzhUpPYw9veJUc0Xx57GtbcZiRzFlTDiRUlsrZ4PvOhYMguanimBE7sRmBaPSPBeUIiqr4yJhVyFSikg==
"@plexusjs/api@1.10.3":
version "1.10.3"
resolved "https://registry.yarnpkg.com/@plexusjs/api/-/api-1.10.3.tgz#32e367333f7a0ab50a4238bad62fed3bfa2f42a0"
integrity sha512-6OdcOA4eUFDnFcpr+Ct9AR3TNvyBtKW3VKQCi/vPzx0uqMCaXr9lgVdNY4TizC1uqQMobevQqVEY9Q1TI0gjWg==
dependencies:
"@plexusjs/utils" "1.9.2"
"@plexusjs/utils" "1.10.3"

"@plexusjs/napi@^1.9.2":
version "1.9.2"
resolved "https://registry.yarnpkg.com/@plexusjs/napi/-/napi-1.9.2.tgz#11ad7fce4c09537eab233ff14128aa202f8a3bb8"
integrity sha512-hGfwADuJE5IS4PyFzGEKr/CMIkL0nuWyVaLP/Cy7jGmdfZPhOSfs5J0MbAJIow+urCjn7fdG1mg7/9leDPo1TQ==
"@plexusjs/napi@^1.10.3":
version "1.10.3"
resolved "https://registry.yarnpkg.com/@plexusjs/napi/-/napi-1.10.3.tgz#b233b884c614df3159a8da2ecdb73d10830ec4de"
integrity sha512-uG3Grl/+dLsAstgVrZCg7kLirlQ82c5e47UNjZlMzk/M0yv7ZGtDljSBoOfcbS3cQKl572HFlJGw5nGDSEfiLw==
dependencies:
"@plexusjs/api" "1.9.2"
"@plexusjs/api" "1.10.3"
cross-fetch "^3.1.5"

"@plexusjs/utils@1.9.2":
version "1.9.2"
resolved "https://registry.yarnpkg.com/@plexusjs/utils/-/utils-1.9.2.tgz#d406d7d0f7f021607e9fbc40f0ad09b56291cba4"
integrity sha512-yn0kUFuU1HUzQSHM5R/omhPfSvb99HP1Gis+/3DeriT1U1YUfZZb315ZXS6mPf9LLuCBPealRPFqQSjD4dD5Ag==
"@plexusjs/utils@1.10.3":
version "1.10.3"
resolved "https://registry.yarnpkg.com/@plexusjs/utils/-/utils-1.10.3.tgz#db27474e18de5aa6ba402015edd4d0850b2d6c68"
integrity sha512-NtKRwGSt3tFoOD8HE1IOm9SiKEdgF3EneyU2sVaIqjQgWtq3zz/i7EGj0rGfNcOlNNz60ded1Zt7X8hFRoinxw==

"@sigstore/bundle@^1.0.0":
version "1.0.0"
Expand Down