From 1cd0cb3cbd2a640a43d7d167a89275f7cfe606f8 Mon Sep 17 00:00:00 2001 From: Mikael Karon Date: Tue, 12 Mar 2024 13:45:00 +0100 Subject: [PATCH 1/3] Register clients before plugins Fixes: https://github.com/platformatic/platformatic/issues/2254 Signed-off-by: Mikael Karon --- packages/service/index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/service/index.js b/packages/service/index.js index 2815996d1..2b44ce196 100644 --- a/packages/service/index.js +++ b/packages/service/index.js @@ -42,6 +42,12 @@ async function platformaticService (app, opts) { await app.register(telemetry, config.telemetry) } + // This must be done before loading the plugins, so they can be + // configured accordingly + if (isKeyEnabled('clients', config)) { + app.register(setupClients, config.clients) + } + if (Array.isArray(beforePlugins)) { for (const plugin of beforePlugins) { app.register(plugin) @@ -83,10 +89,6 @@ async function platformaticService (app, opts) { } }) - if (isKeyEnabled('clients', config)) { - app.register(setupClients, config.clients) - } - if (isKeyEnabled('cors', config.server)) { app.register(setupCors, config.server.cors) } From 8d48e6bead9a26dffa8d84f32f97cf3ce372c23a Mon Sep 17 00:00:00 2001 From: Mikael Karon Date: Tue, 12 Mar 2024 18:06:54 +0100 Subject: [PATCH 2/3] test: add tests for client loading before plugin Signed-off-by: Mikael Karon --- .../hello-client-from-plugin/hello.d.ts | 34 +++++++++++++++++++ .../hello.openapi.json | 22 ++++++++++++ .../platformatic.service.json | 21 ++++++++++++ .../hello-client-from-plugin/plugin.js | 9 +++++ packages/service/test/clients.test.js | 23 +++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 packages/service/fixtures/hello-client-from-plugin/hello.d.ts create mode 100644 packages/service/fixtures/hello-client-from-plugin/hello.openapi.json create mode 100644 packages/service/fixtures/hello-client-from-plugin/platformatic.service.json create mode 100644 packages/service/fixtures/hello-client-from-plugin/plugin.js diff --git a/packages/service/fixtures/hello-client-from-plugin/hello.d.ts b/packages/service/fixtures/hello-client-from-plugin/hello.d.ts new file mode 100644 index 000000000..75a85c9ca --- /dev/null +++ b/packages/service/fixtures/hello-client-from-plugin/hello.d.ts @@ -0,0 +1,34 @@ +import { FastifyPluginAsync } from 'fastify' + +interface GetRequest { +} + +interface GetResponse { +} + +interface Hello { + get(req: GetRequest): Promise; +} + +type HelloPlugin = FastifyPluginAsync> + +declare module 'fastify' { + interface FastifyInstance { + 'hello': Hello; + } + + interface FastifyRequest { + 'hello': Hello; + } +} + +declare namespace hello { + export interface HelloOptions { + url: string + } + export const hello: HelloPlugin; + export { hello as default }; +} + +declare function hello(...params: Parameters): ReturnType; +export = hello; diff --git a/packages/service/fixtures/hello-client-from-plugin/hello.openapi.json b/packages/service/fixtures/hello-client-from-plugin/hello.openapi.json new file mode 100644 index 000000000..671acffd8 --- /dev/null +++ b/packages/service/fixtures/hello-client-from-plugin/hello.openapi.json @@ -0,0 +1,22 @@ +{ + "openapi": "3.0.3", + "info": { + "title": "Platformatic", + "description": "This is a service built on top of Platformatic", + "version": "1.0.0" + }, + "components": { + "schemas": {} + }, + "paths": { + "/": { + "get": { + "responses": { + "200": { + "description": "Default Response" + } + } + } + } + } +} \ No newline at end of file diff --git a/packages/service/fixtures/hello-client-from-plugin/platformatic.service.json b/packages/service/fixtures/hello-client-from-plugin/platformatic.service.json new file mode 100644 index 000000000..d147ec2e0 --- /dev/null +++ b/packages/service/fixtures/hello-client-from-plugin/platformatic.service.json @@ -0,0 +1,21 @@ +{ + "server": { + "hostname": "127.0.0.1", + "port": 0, + "logger": { + "level": "warn", + "name": "hello client" + } + }, + "plugins": { + "paths": ["./plugin.js"] + }, + "clients": [{ + "schema": "./hello.openapi.json", + "name": "hello", + "type": "openapi", + "url": "{PLT_CLIENT_URL}" + }], + "metrics": false, + "watch": false +} diff --git a/packages/service/fixtures/hello-client-from-plugin/plugin.js b/packages/service/fixtures/hello-client-from-plugin/plugin.js new file mode 100644 index 000000000..5182582f8 --- /dev/null +++ b/packages/service/fixtures/hello-client-from-plugin/plugin.js @@ -0,0 +1,9 @@ +'use default' + +/** @type {import('fastify').FastifyPluginAsync<{ optionA: boolean, optionB: string }>} */ +module.exports = async function (app) { + const result = app.hello.get() + app.get('/', async () => { + return result + }) +} diff --git a/packages/service/test/clients.test.js b/packages/service/test/clients.test.js index 63da41a72..d08aa7d0e 100644 --- a/packages/service/test/clients.test.js +++ b/packages/service/test/clients.test.js @@ -87,3 +87,26 @@ test('client is loaded dependencyless', async (t) => { const data = await res.body.json() assert.deepStrictEqual(data, { hello: 'world' }) }) + +test('client is loaded before plugins', async (t) => { + const app1 = await buildServer(join(__dirname, '..', 'fixtures', 'hello', 'warn-log.service.json')) + + t.after(async () => { + await app1.close() + }) + await app1.start() + + process.env.PLT_CLIENT_URL = app1.url + + const app2 = await buildServer(join(__dirname, '..', 'fixtures', 'hello-client-from-plugin', 'platformatic.service.json')) + + t.after(async () => { + await app2.close() + }) + await app2.start() + + const res = await request(`${app2.url}/`) + assert.strictEqual(res.statusCode, 200, 'status code') + const data = await res.body.json() + assert.deepStrictEqual(data, { hello: 'world' }) +}) From 4310d6f81553b3cc2e000f6793224486f68eb46a Mon Sep 17 00:00:00 2001 From: Mikael Karon Date: Wed, 13 Mar 2024 14:39:25 +0100 Subject: [PATCH 3/3] test: fix clients-before-plugins test Signed-off-by: Mikael Karon --- .../service/fixtures/hello-client-from-plugin/plugin.js | 6 +++--- packages/service/test/clients.test.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/service/fixtures/hello-client-from-plugin/plugin.js b/packages/service/fixtures/hello-client-from-plugin/plugin.js index 5182582f8..2889f97f2 100644 --- a/packages/service/fixtures/hello-client-from-plugin/plugin.js +++ b/packages/service/fixtures/hello-client-from-plugin/plugin.js @@ -2,8 +2,8 @@ /** @type {import('fastify').FastifyPluginAsync<{ optionA: boolean, optionB: string }>} */ module.exports = async function (app) { - const result = app.hello.get() - app.get('/', async () => { - return result + const hasConfig = !!app.configureHello + app.get('/', async (req) => { + return { ...await req.hello.get(), hasConfig } }) } diff --git a/packages/service/test/clients.test.js b/packages/service/test/clients.test.js index d08aa7d0e..f8e9b3af0 100644 --- a/packages/service/test/clients.test.js +++ b/packages/service/test/clients.test.js @@ -108,5 +108,5 @@ test('client is loaded before plugins', async (t) => { const res = await request(`${app2.url}/`) assert.strictEqual(res.statusCode, 200, 'status code') const data = await res.body.json() - assert.deepStrictEqual(data, { hello: 'world' }) + assert.deepStrictEqual(data, { hello: 'world', hasConfig: true }) })