Skip to content

Commit 5846f73

Browse files
authored
fix: types for context (#276)
1 parent 0c5cae1 commit 5846f73

File tree

8 files changed

+52
-89
lines changed

8 files changed

+52
-89
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ please see the [Podium documentation].
1212
## Installation
1313

1414
```bash
15-
$ npm install @podium/fastify-layout
15+
npm install @podium/fastify-layout
1616
```
1717

1818
## Simple usage
@@ -30,12 +30,11 @@ const layout = new Layout({
3030
pathname: '/',
3131
logger: console,
3232
name: 'layout',
33-
3433
});
3534

3635
const podlet = layout.client.register({
3736
name: 'myPodlet',
38-
uri: 'http://localhost:7100/manifest.json'
37+
uri: 'http://localhost:7100/manifest.json',
3938
});
4039

4140
app.register(FastifyLayout, layout);
@@ -44,17 +43,18 @@ app.get(layout.pathname(), async (request, reply) => {
4443
const incoming = reply.app.podium;
4544
const result = await podlet.fetch(incoming);
4645
reply.podiumSend(result.content);
46+
await reply;
4747
});
4848

4949
const start = async () => {
5050
try {
51-
await app.listen(7000);
51+
await app.listen({ port: 7000 });
5252
app.log.info(`server listening on ${app.server.address().port}`);
5353
} catch (err) {
5454
app.log.error(err);
5555
process.exit(1);
5656
}
57-
}
57+
};
5858
start();
5959
```
6060

@@ -121,4 +121,4 @@ SOFTWARE.
121121
[document template]: https://podium-lib.io/docs/api/document 'document template'
122122
[@podium/context]: https://github.com/podium-lib/context '@podium/context'
123123
[@podium/layout]: https://github.com/podium-lib/layout '@podium/layout'
124-
[fastify]: https://www.fastify.io/ 'Fastify'
124+
[fastify]: https://www.fastify.io/ 'Fastify'

example/server.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

lib/layout-plugin.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import { HttpIncoming, pathnameBuilder } from '@podium/utils';
22
import fp from 'fastify-plugin';
33

44
export default fp(
5-
(fastify, options, done) => {
6-
const layout = /** @type {import('@podium/layout').default} */ (
7-
options
8-
);
9-
5+
/**
6+
* @type {import('fastify').FastifyPluginCallback<import('@podium/layout').default>}
7+
*/
8+
(fastify, layout, done) => {
109
// Decorate reply with .app.podium we can write to throughout the request
1110
fastify.decorateReply('app', null);
1211
fastify.addHook('onRequest', async (request, reply) => {

package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@
3232
"scripts": {
3333
"lint": "eslint .",
3434
"lint:fix": "eslint . --fix",
35-
"test": "run-s test:unit test:types",
36-
"test:unit": "tap --disable-coverage --allow-empty-coverage",
37-
"test:types": "tsc --project tsconfig.test.json",
35+
"test": "tap --disable-coverage --allow-empty-coverage",
3836
"test:coverage": "tap",
3937
"types": "run-s types:tsc types:fixup",
40-
"types:tsc": "tsc --declaration --emitDeclarationOnly",
38+
"types:tsc": "tsc",
4139
"types:fixup": "node ./fixup.js"
4240
},
4341
"author": "Trygve Lie",
@@ -48,7 +46,7 @@
4846
"devDependencies": {
4947
"@babel/eslint-parser": "7.24.7",
5048
"@fastify/formbody": "7.4.0",
51-
"@podium/layout": "5.1.14",
49+
"@podium/layout": "5.2.7",
5250
"@podium/test-utils": "3.0.6",
5351
"@semantic-release/changelog": "6.0.3",
5452
"@semantic-release/git": "10.0.1",
@@ -61,7 +59,7 @@
6159
"npm-run-all2": "5.0.2",
6260
"prettier": "3.3.2",
6361
"semantic-release": "22.0.12",
64-
"tap": "19.2.5",
62+
"tap": "21.0.1",
6563
"typescript": "5.5.4"
6664
}
6765
}

tests/layout-plugin.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/// <reference path="../types/layout-plugin.d.ts" />
21
import { PodletServer, request } from '@podium/test-utils';
32
import fastify from 'fastify';
43
import Layout from '@podium/layout';

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"checkJs": true,
88
"allowJs": true,
99
"moduleResolution": "nodenext",
10+
"emitDeclarationOnly": true,
1011
"declaration": true,
11-
"skipLibCheck": true,
1212
"allowSyntheticDefaultImports": true,
1313
"outDir": "types"
1414
},

tsconfig.test.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

types/podium.d.ts

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
1-
import { HttpIncoming } from "@podium/utils";
1+
import { HttpIncoming } from '@podium/utils';
22

3-
declare module "fastify" {
4-
interface PodiumParams {}
3+
declare module 'fastify' {
4+
interface PodiumParams {}
55

6-
interface PodiumLocals {
7-
/**
8-
* @see https://podium-lib.io/docs/api/incoming
9-
*/
10-
podium: HttpIncoming;
11-
params: PodiumParams;
12-
}
6+
interface PodiumHttpIncomingParameters {
7+
[key: string]: unknown;
8+
}
139

10+
// @podium/layout declares what's on the context. We use the same interface names here to inherit them.
1411

15-
interface FastifyReply {
16-
app: PodiumLocals;
17-
/**
18-
* This method wraps the provided fragment in a default HTML document before dispatching.
19-
*
20-
* @param markup The HTML contents of the document
21-
* @param args Parameters sent to the template function
22-
* @returns {void} Sends the FastifyReply
23-
*
24-
* @see https://podium-lib.io/docs/api/layout#respodiumsendfragment
25-
*/
26-
podiumSend: (markup: unknown, ...args: unknown[]) => void;
27-
}
12+
interface PodiumHttpIncomingContext {
13+
[key: string]: unknown;
14+
}
15+
16+
interface PodiumHttpIncomingViewParameters {
17+
[key: string]: unknown;
18+
}
19+
20+
interface PodiumLocals {
21+
podium: HttpIncoming<
22+
PodiumHttpIncomingParameters,
23+
PodiumHttpIncomingContext,
24+
PodiumHttpIncomingViewParameters
25+
>;
26+
params: PodiumParams;
27+
}
28+
29+
interface FastifyReply {
30+
app: PodiumLocals;
31+
/**
32+
* This method wraps the provided fragment in a default HTML document before dispatching.
33+
*
34+
* @param markup The HTML contents of the document
35+
* @param args Parameters sent to the template function
36+
* @returns {void} Sends the FastifyReply
37+
*
38+
* @see https://podium-lib.io/docs/api/layout#respodiumsendfragment
39+
*/
40+
podiumSend: (markup: unknown, ...args: unknown[]) => void;
41+
}
2842
}

0 commit comments

Comments
 (0)