@platformatic/composer 2.72.0 → 3.0.0-alpha.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.
Files changed (58) hide show
  1. package/config.d.ts +1 -19
  2. package/eslint.config.js +11 -2
  3. package/index.d.ts +58 -17
  4. package/index.js +29 -212
  5. package/lib/application.js +186 -0
  6. package/lib/commands/index.js +15 -0
  7. package/lib/commands/openapi-fetch-schemas.js +47 -0
  8. package/lib/composer-hook.js +9 -9
  9. package/lib/errors.js +15 -10
  10. package/lib/generator.js +158 -0
  11. package/lib/graphql-fetch.js +44 -46
  12. package/lib/graphql-generator.js +12 -10
  13. package/lib/graphql.js +13 -9
  14. package/lib/{proxy/not-host-constraints.js → not-host-constraints.js} +3 -5
  15. package/lib/openapi-composer.js +39 -41
  16. package/lib/openapi-config-schema.js +26 -30
  17. package/lib/openapi-generator.js +115 -112
  18. package/lib/openapi-load-config.js +14 -14
  19. package/lib/openapi-modifier.js +12 -21
  20. package/lib/openapi-scalar.js +3 -5
  21. package/lib/proxy.js +21 -34
  22. package/lib/{root-endpoint/index.js → root.js} +12 -12
  23. package/lib/schema.js +34 -24
  24. package/lib/stackable.js +29 -39
  25. package/lib/upgrade.js +6 -8
  26. package/lib/utils.js +5 -16
  27. package/lib/versions/2.0.0.js +4 -6
  28. package/package.json +14 -18
  29. package/schema.json +2 -73
  30. package/.c8rc +0 -6
  31. package/composer.mjs +0 -54
  32. package/help/create.txt +0 -11
  33. package/help/help.txt +0 -7
  34. package/help/openapi schemas fetch.txt +0 -9
  35. package/help/start.txt +0 -54
  36. package/index.test-d.ts +0 -23
  37. package/lib/create.mjs +0 -84
  38. package/lib/generator/README.md +0 -30
  39. package/lib/generator/composer-generator.d.ts +0 -11
  40. package/lib/generator/composer-generator.js +0 -128
  41. package/lib/metrics.js +0 -12
  42. package/lib/openapi-fetch-schemas.mjs +0 -61
  43. /package/{lib/root-endpoint/public → public}/images/dark_mode.svg +0 -0
  44. /package/{lib/root-endpoint/public → public}/images/ellipse.svg +0 -0
  45. /package/{lib/root-endpoint/public → public}/images/external-link.svg +0 -0
  46. /package/{lib/root-endpoint/public → public}/images/favicon.ico +0 -0
  47. /package/{lib/root-endpoint/public → public}/images/graphiql.svg +0 -0
  48. /package/{lib/root-endpoint/public → public}/images/graphql.svg +0 -0
  49. /package/{lib/root-endpoint/public → public}/images/light_mode.svg +0 -0
  50. /package/{lib/root-endpoint/public → public}/images/openapi.svg +0 -0
  51. /package/{lib/root-endpoint/public → public}/images/platformatic-logo-dark.svg +0 -0
  52. /package/{lib/root-endpoint/public → public}/images/platformatic-logo-light.svg +0 -0
  53. /package/{lib/root-endpoint/public → public}/images/reverse-proxy.svg +0 -0
  54. /package/{lib/root-endpoint/public → public}/images/triangle_dark.svg +0 -0
  55. /package/{lib/root-endpoint/public → public}/images/triangle_light.svg +0 -0
  56. /package/{lib/root-endpoint/public → public}/index.html +0 -0
  57. /package/{lib/root-endpoint/public → public}/index.njk +0 -0
  58. /package/{lib/root-endpoint/public → public}/main.css +0 -0
package/lib/create.mjs DELETED
@@ -1,84 +0,0 @@
1
- 'use strict'
2
- import minimist from 'minimist'
3
- import { Generator } from './generator/composer-generator.js'
4
- import { join } from 'node:path'
5
- import { getPkgManager } from '@platformatic/utils'
6
- import { execa } from 'execa'
7
- import ora from 'ora'
8
- import { Table } from 'console-table-printer'
9
- import pino from 'pino'
10
- import pinoPretty from 'pino-pretty'
11
-
12
- function printAppSummary (args, logger) {
13
- logger.info('Creating a Platformatic Composer app with this config: ')
14
- const table = [
15
- { config: 'Directory', value: args.dir },
16
- { config: 'Language', value: args.typescript ? 'Typescript' : 'Javascript' },
17
- { config: 'Init Git Repository', value: args.git },
18
- { config: 'Install Dependencies', value: args.install },
19
- ]
20
-
21
- const p = new Table({
22
- columns: [
23
- { name: 'config', alignment: 'right' },
24
- { name: 'value', alignment: 'left' },
25
- ],
26
- })
27
-
28
- p.addRows(table)
29
- p.printTable()
30
- }
31
- async function createComposer (_args) {
32
- const stream = pinoPretty({
33
- translateTime: 'SYS:HH:MM:ss',
34
- ignore: 'hostname,pid',
35
- minimumLevel: 'debug',
36
- sync: true,
37
- })
38
-
39
- const logger = pino(stream)
40
-
41
- const args = minimist(process.argv.slice(2), {
42
- string: ['dir', 'port', 'hostname', 'git'],
43
- boolean: ['typescript', 'install'],
44
- default: {
45
- dir: join(process.cwd(), 'platformatic-composer'),
46
- port: 3042,
47
- hostname: '0.0.0.0',
48
- plugin: true,
49
- typescript: false,
50
- git: false,
51
- install: true,
52
- },
53
-
54
- })
55
-
56
- printAppSummary(args, logger)
57
-
58
- const gen = new Generator({})
59
- gen.setConfig({
60
- port: args.port,
61
- hostname: args.hostname,
62
- plugin: args.plugin,
63
- tests: args.plugin,
64
- typescript: args.typescript,
65
- initGitRepository: args.git,
66
- targetDirectory: args.dir,
67
- })
68
-
69
- try {
70
- await gen.run()
71
- if (args.install) {
72
- const pkgManager = getPkgManager()
73
- const spinner = ora('Installing dependencies...').start()
74
- await execa(pkgManager, ['install'], { cwd: args.dir })
75
- spinner.succeed()
76
- }
77
-
78
- logger.info('Done! 🎉')
79
- } catch (err) {
80
- logger.error(err.message)
81
- }
82
- }
83
-
84
- export { createComposer }
@@ -1,30 +0,0 @@
1
- # Platformatic Composer API
2
-
3
- This is a generated [Platformatic Composer](https://docs.platformatic.dev/docs/composer/overview) application.
4
-
5
- ## Requirements
6
-
7
- Platformatic supports macOS, Linux and Windows ([WSL](https://docs.microsoft.com/windows/wsl/) recommended).
8
- You'll need to have [Node.js](https://nodejs.org/) >= v18.8.0 or >= v20.6.0
9
-
10
- ## Setup
11
-
12
- 1. Install dependencies:
13
-
14
- ```bash
15
- npm install
16
- ```
17
-
18
- ## Usage
19
-
20
- Run the API with:
21
-
22
- ```bash
23
- npm start
24
- ```
25
-
26
- ### Explore
27
- - ⚡ The Platformatic Composer server is running at http://localhost:3042/
28
- - 📔 View the REST API's Swagger documentation at http://localhost:3042/documentation/
29
-
30
-
@@ -1,11 +0,0 @@
1
- import { BaseGenerator } from "@platformatic/generators";
2
- import RuntimeGenerator from "../../../runtime/lib/generator/runtime-generator";
3
-
4
- export namespace ComposerGenerator {
5
- export class ComposerGenerator extends BaseGenerator {
6
- runtime: RuntimeGenerator
7
-
8
- setRuntime(runtime: RuntimeGenerator): void
9
- }
10
-
11
- }
@@ -1,128 +0,0 @@
1
- 'use strict'
2
-
3
- const { BaseGenerator } = require('@platformatic/generators')
4
- const { join } = require('node:path')
5
- const { readFile } = require('node:fs/promises')
6
-
7
- class ComposerGenerator extends BaseGenerator {
8
- constructor (opts) {
9
- super({
10
- ...opts,
11
- module: '@platformatic/composer',
12
- })
13
- this.runtime = null
14
- }
15
-
16
- getDefaultConfig () {
17
- const defaultBaseConfig = super.getDefaultConfig()
18
- return {
19
- ...defaultBaseConfig,
20
- plugin: false,
21
- routes: false,
22
- tests: false,
23
- }
24
- }
25
-
26
- async _getConfigFileContents () {
27
- const template = {
28
- $schema: `https://schemas.platformatic.dev/@platformatic/composer/${this.platformaticVersion}.json`,
29
- composer: {
30
- services: [
31
- {
32
- id: 'example',
33
- origin: `{${this.getEnvVarName('PLT_EXAMPLE_ORIGIN')}}`,
34
- openapi: {
35
- url: '/documentation/json',
36
- },
37
- },
38
- ],
39
- refreshTimeout: 1000,
40
- },
41
- watch: true,
42
- }
43
- if (this.runtime !== null) {
44
- template.composer.services = this.runtime.services
45
- .filter(serviceMeta => serviceMeta.service.module !== '@platformatic/composer')
46
- .map((serviceMeta) => {
47
- return {
48
- id: serviceMeta.name,
49
- openapi: {
50
- url: '/documentation/json',
51
- prefix: `/${serviceMeta.name}`,
52
- },
53
- }
54
- })
55
- }
56
-
57
- if (this.config.plugin) {
58
- template.plugins = {
59
- paths: [
60
- {
61
- path: './plugins',
62
- encapsulate: false,
63
- },
64
- './routes',
65
- ],
66
- typescript: `{${this.getEnvVarName('PLT_TYPESCRIPT')}}`,
67
- }
68
- }
69
-
70
- if (!this.config.isRuntimeContext) {
71
- template.server = {
72
- hostname: '{PLT_SERVER_HOSTNAME}',
73
- port: '{PORT}',
74
- logger: {
75
- level: '{PLT_SERVER_LOGGER_LEVEL}',
76
- },
77
- }
78
- }
79
-
80
- return template
81
- }
82
-
83
- async _beforePrepare () {
84
- if (!this.config.isUpdating) {
85
- if (!this.config.isRuntimeContext) {
86
- this.addEnvVars({
87
- PLT_SERVER_HOSTNAME: this.config.hostname,
88
- PLT_SERVER_LOGGER_LEVEL: 'info',
89
- PORT: 3042,
90
- }, { overwrite: false, default: true })
91
- }
92
-
93
- this.addEnvVars({
94
- PLT_TYPESCRIPT: this.config.typescript,
95
- PLT_EXAMPLE_ORIGIN: 'http://127.0.0.1:3043',
96
- }, { overwrite: false, default: true })
97
-
98
- this.config.dependencies = {
99
- '@platformatic/composer': `^${this.platformaticVersion}`,
100
- }
101
- }
102
- }
103
-
104
- async _afterPrepare () {
105
- if (!this.config.isUpdating) {
106
- const GLOBAL_TYPES_TEMPLATE = `
107
- import { FastifyInstance } from 'fastify'
108
- import { PlatformaticApp, PlatformaticComposerConfig } from '@platformatic/composer'
109
-
110
- declare module 'fastify' {
111
- interface FastifyInstance {
112
- platformatic: PlatformaticApp<PlatformaticComposerConfig>
113
- }
114
- }
115
- `
116
- this.addFile({ path: '', file: 'global.d.ts', contents: GLOBAL_TYPES_TEMPLATE })
117
- this.addFile({ path: '', file: 'README.md', contents: await readFile(join(__dirname, 'README.md'), 'utf-8') })
118
- }
119
- }
120
-
121
- setRuntime (runtime) {
122
- this.runtime = runtime
123
- }
124
- }
125
-
126
- module.exports = ComposerGenerator
127
- module.exports.ComposerGenerator = ComposerGenerator
128
- module.exports.Generator = ComposerGenerator
package/lib/metrics.js DELETED
@@ -1,12 +0,0 @@
1
- export function initMetrics (prometheus) {
2
- if (!prometheus?.registry || !prometheus?.client) return null
3
- const { client, registry } = prometheus
4
-
5
- return {
6
- activeWsConnections: new client.Gauge({
7
- name: 'active_ws_composer_connections',
8
- help: 'Active Websocket composer connections in "@platformatic/composer"',
9
- registers: [registry]
10
- })
11
- }
12
- }
@@ -1,61 +0,0 @@
1
- import { writeFile } from 'node:fs/promises'
2
-
3
- import pino from 'pino'
4
- import pretty from 'pino-pretty'
5
- import { request } from 'undici'
6
-
7
- import { loadConfig } from '@platformatic/config'
8
- import { platformaticComposer } from '../index.js'
9
- import errors from '../lib/errors.js'
10
- import { prefixWithSlash } from './utils.js'
11
-
12
- async function fetchOpenApiSchema (service) {
13
- const { origin, openapi } = service
14
-
15
- const openApiUrl = origin + prefixWithSlash(openapi.url)
16
- const { statusCode, body } = await request(openApiUrl)
17
-
18
- if (statusCode !== 200 && statusCode !== 201) {
19
- throw new errors.FailedToFetchOpenAPISchemaError(openApiUrl)
20
- }
21
- const schema = await body.json()
22
-
23
- if (openapi.file !== undefined) {
24
- await writeFile(openapi.file, JSON.stringify(schema, null, 2))
25
- }
26
-
27
- return schema
28
- }
29
-
30
- export default async function fetchOpenApiSchemas (_args) {
31
- const logger = pino(pretty({
32
- translateTime: 'SYS:HH:MM:ss',
33
- ignore: 'hostname,pid',
34
- }))
35
-
36
- const { configManager } = await loadConfig({}, _args, platformaticComposer)
37
- await configManager.parseAndValidate()
38
- const config = configManager.current
39
- const { services } = config.composer
40
-
41
- const servicesWithValidOpenApi = services
42
- .filter(({ openapi }) => openapi && openapi.url && openapi.file)
43
-
44
- const fetchOpenApiRequests = servicesWithValidOpenApi
45
- .map(service => fetchOpenApiSchema(service))
46
-
47
- const fetchOpenApiResults = await Promise.allSettled(fetchOpenApiRequests)
48
- fetchOpenApiResults.forEach((result, index) => {
49
- const serviceId = servicesWithValidOpenApi[index].id
50
- if (result.status === 'rejected') {
51
- logger.error(`Failed to fetch OpenAPI schema for service with id ${serviceId}: ${result.reason}`)
52
- } else {
53
- logger.info(`Successfully fetched OpenAPI schema for service with id ${serviceId}`)
54
- }
55
- })
56
- }
57
-
58
- export {
59
- fetchOpenApiSchema,
60
- fetchOpenApiSchemas,
61
- }
File without changes
File without changes
File without changes