Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register clients before plugins #2255

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions 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<GetResponse>;
}

type HelloPlugin = FastifyPluginAsync<NonNullable<hello.HelloOptions>>

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<HelloPlugin>): ReturnType<HelloPlugin>;
export = hello;
@@ -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"
}
}
}
}
}
}
@@ -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
}
9 changes: 9 additions & 0 deletions 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()
mikaelkaron marked this conversation as resolved.
Show resolved Hide resolved
app.get('/', async () => {
return result
})
}
10 changes: 6 additions & 4 deletions packages/service/index.js
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
23 changes: 23 additions & 0 deletions packages/service/test/clients.test.js
Expand Up @@ -87,3 +87,26 @@
const data = await res.body.json()
assert.deepStrictEqual(data, { hello: 'world' })
})

test('client is loaded before plugins', async (t) => {

Check failure on line 91 in packages/service/test/clients.test.js

View workflow job for this annotation

GitHub Actions / ci-service (20, ubuntu-latest)

client is loaded before plugins

[Error [ERR_TEST_FAILURE]: Cannot read properties of undefined (reading 'get')] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: TypeError [Error]: Cannot read properties of undefined (reading 'get') at module.exports (/home/runner/work/platformatic/platformatic/packages/service/fixtures/hello-client-from-plugin/plugin.js:5:28) at Plugin.exec (/home/runner/work/platformatic/platformatic/node_modules/.pnpm/avvio@8.3.0/node_modules/avvio/lib/plugin.js:122:28) at Boot._loadPlugin (/home/runner/work/platformatic/platformatic/node_modules/.pnpm/avvio@8.3.0/node_modules/avvio/boot.js:423:10) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) }

Check failure on line 91 in packages/service/test/clients.test.js

View workflow job for this annotation

GitHub Actions / ci-service (18, ubuntu-latest)

client is loaded before plugins

[Error [ERR_TEST_FAILURE]: Cannot read properties of undefined (reading 'get')] { failureType: 'testCodeFailure', cause: TypeError [Error]: Cannot read properties of undefined (reading 'get') at module.exports (/home/runner/work/platformatic/platformatic/packages/service/fixtures/hello-client-from-plugin/plugin.js:5:28) at Plugin.exec (/home/runner/work/platformatic/platformatic/node_modules/.pnpm/avvio@8.3.0/node_modules/avvio/lib/plugin.js:122:28) at Boot._loadPlugin (/home/runner/work/platformatic/platformatic/node_modules/.pnpm/avvio@8.3.0/node_modules/avvio/boot.js:423:10) at process.processTicksAndRejections (node:internal/process/task_queues:82:21), code: 'ERR_TEST_FAILURE' }
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' })
})