@platformatic/service 3.4.1 → 3.5.0

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 (54) hide show
  1. package/LICENSE +1 -1
  2. package/config.d.ts +450 -94
  3. package/eslint.config.js +4 -6
  4. package/index.d.ts +55 -48
  5. package/index.js +44 -179
  6. package/lib/application.js +35 -0
  7. package/lib/capability.js +281 -0
  8. package/lib/compile.js +2 -52
  9. package/lib/generator.js +426 -0
  10. package/lib/plugins/cors.js +5 -8
  11. package/lib/plugins/graphql.js +16 -14
  12. package/lib/plugins/health-check.js +6 -8
  13. package/lib/plugins/openapi.js +43 -32
  14. package/lib/plugins/plugins.js +6 -53
  15. package/lib/{root-endpoint/index.js → plugins/root.js} +9 -8
  16. package/lib/plugins/sandbox-wrapper.js +65 -63
  17. package/lib/schema.js +1075 -203
  18. package/lib/upgrade.js +6 -8
  19. package/lib/utils.js +30 -83
  20. package/lib/versions/0.16.0.js +14 -15
  21. package/lib/versions/{from-zero-twenty-eight-to-will-see.js → 0.28.0.js} +3 -6
  22. package/lib/versions/2.0.0.js +4 -7
  23. package/lib/versions/3.0.0.js +14 -0
  24. package/package.json +28 -36
  25. package/schema.json +1452 -165
  26. package/tsconfig.json +16 -6
  27. package/.c8rc +0 -6
  28. package/help/compile.txt +0 -19
  29. package/help/create.txt +0 -11
  30. package/help/help.txt +0 -8
  31. package/help/schema.txt +0 -9
  32. package/help/start.txt +0 -23
  33. package/index.test-d.ts +0 -107
  34. package/lib/create.mjs +0 -85
  35. package/lib/gen-schema.js +0 -15
  36. package/lib/gen-types.mjs +0 -38
  37. package/lib/generator/README.md +0 -31
  38. package/lib/generator/service-generator.d.ts +0 -11
  39. package/lib/generator/service-generator.js +0 -126
  40. package/lib/openapi-schema-defs.js +0 -1108
  41. package/lib/plugins/clients.js +0 -16
  42. package/lib/plugins/metrics.js +0 -244
  43. package/lib/plugins/typescript.js +0 -20
  44. package/lib/stackable.js +0 -306
  45. package/lib/start.js +0 -175
  46. package/service.mjs +0 -71
  47. /package/{lib/root-endpoint/public → public}/images/dark_mode.svg +0 -0
  48. /package/{lib/root-endpoint/public → public}/images/favicon.ico +0 -0
  49. /package/{lib/root-endpoint/public → public}/images/light_mode.svg +0 -0
  50. /package/{lib/root-endpoint/public → public}/images/platformatic-logo-dark.svg +0 -0
  51. /package/{lib/root-endpoint/public → public}/images/platformatic-logo-light.svg +0 -0
  52. /package/{lib/root-endpoint/public → public}/images/triangle_dark.svg +0 -0
  53. /package/{lib/root-endpoint/public → public}/images/triangle_light.svg +0 -0
  54. /package/{lib/root-endpoint/public → public}/index.html +0 -0
package/tsconfig.json CHANGED
@@ -1,12 +1,22 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "module": "CommonJS",
4
- "moduleResolution": "node",
3
+ "module": "esnext",
5
4
  "esModuleInterop": true,
6
- "allowSyntheticDefaultImports": true,
7
- "target": "es2019",
8
- "outDir": "build",
5
+ "lib": ["es2022"],
6
+ "target": "esnext",
9
7
  "sourceMap": true,
10
- "strictNullChecks": true
8
+ "pretty": true,
9
+ "noEmitOnError": true,
10
+ "incremental": true,
11
+ "strict": true,
12
+ "outDir": "dist",
13
+ "skipLibCheck": true
14
+ },
15
+ "watchOptions": {
16
+ "watchFile": "fixedPollingInterval",
17
+ "watchDirectory": "fixedPollingInterval",
18
+ "fallbackPolling": "dynamicPriority",
19
+ "synchronousWatchDirectory": true,
20
+ "excludeDirectories": ["**/node_modules", "dist"]
11
21
  }
12
22
  }
package/.c8rc DELETED
@@ -1,6 +0,0 @@
1
- {
2
- "exclude": [
3
- "fixtures/*",
4
- "test/*"
5
- ]
6
- }
package/help/compile.txt DELETED
@@ -1,19 +0,0 @@
1
- Compile typescript plugins.
2
-
3
- ``` bash
4
- $ platformatic service compile
5
- ```
6
-
7
- As a result of executing this command, Platformatic Service will compile typescript
8
- plugins in the `outDir` directory.
9
-
10
- Using the `--clean` flag, the outDir directory will be removed before the new compilation process starts.
11
-
12
- If not specified, the configuration will be loaded from any of the following, in the current directory.
13
-
14
- * `platformatic.json`, or
15
- * `platformatic.yml`, or
16
- * `platformatic.tml`
17
-
18
- You can find more details about the configuration format here:
19
- * [Platformatic Service Configuration](https://docs.platformatic.dev/docs/service/configuration)
package/help/create.txt DELETED
@@ -1,11 +0,0 @@
1
- Creates a new Platformatic Service application.
2
-
3
- Options are
4
-
5
- * `dir <string>` - the directory where to create the project (Default: `process.cwd() + 'platformatic-composer'`)
6
- * `port <string>` - the port where the application will listen (Default: `3042`)
7
- * `hostname <string>` - the hostname where the application will listen (Default: `0.0.0.0`)
8
- * `git <boolean>` - Init the git repository (Default: `true`)
9
- * `typescript <boolean>` - Use Typescript (Default: `false`)
10
- * `install <boolean>` - Run or not `npm install` after creating the files (Default: `true`)
11
- * `plugin <boolean>` - Creates a sample plugin and tests (Default: `true`)
package/help/help.txt DELETED
@@ -1,8 +0,0 @@
1
- Available commands:
2
-
3
- * `create` - creates a new Platformatic Service application.
4
- * `help` - show this help message.
5
- * `help <command>` - show more information about a command.
6
- * `start` - start the server.
7
- * `schema config` - generate the schema configuration file.
8
- * `compile` - compile the typescript files.
package/help/schema.txt DELETED
@@ -1,9 +0,0 @@
1
- Update the config schema file:
2
-
3
- * `schema config` - update the JSON schema config available on `platformatic.json`
4
-
5
- Your configuration on `platformatic.json` has a schema defined to improve the developer experience and avoid mistakes when updating the configuration of Platformatic Service.
6
- When you initialize a new Platformatic service (f.e. running `npm create platformatic@latest`), a new JSON `$schema` property is added in the `platformatic.json` config. This can allow your IDE to add suggestions (f.e. mandatory/missing fields, types, default values) by opening the config in `platformatic.service.json`.
7
- Running `platformatic service schema config` you can update your schema so that it matches well the latest changes available on your config.
8
-
9
-
package/help/start.txt DELETED
@@ -1,23 +0,0 @@
1
- Start the Platformatic Service with the following command:
2
-
3
- ``` bash
4
- $ platformatic service start
5
- ```
6
-
7
- You will need a configuration file. Here is an example to get you started,
8
- save the following as `platformatic.json`:
9
-
10
- ``` json
11
- {
12
- "server": {
13
- "hostname": "127.0.0.1",
14
- "port": 0,
15
- "logger": {
16
- "level": "info"
17
- }
18
- },
19
- "plugin": {
20
- "path": "./plugin.js"
21
- }
22
- }
23
- ```
package/index.test-d.ts DELETED
@@ -1,107 +0,0 @@
1
- import { expectType, expectError } from 'tsd'
2
- import { FastifyInstance } from 'fastify'
3
- import ConfigManager, { StackableInterface } from '@platformatic/config'
4
- import { OpenAPI } from 'openapi-types'
5
- import type { MercuriusPlugin } from 'mercurius'
6
- import { PlatformaticService } from './config'
7
- import { BaseGenerator } from '@platformatic/generators'
8
- import {
9
- start,
10
- buildServer,
11
- PlatformaticApp,
12
- platformaticService,
13
- Stackable,
14
- Generator,
15
- PlatformaticServiceConfig,
16
- } from '.'
17
-
18
- declare module 'fastify' {
19
- interface FastifyInstance {
20
- platformatic: PlatformaticApp<PlatformaticService>
21
- }
22
- }
23
-
24
- const server = await buildServer({})
25
-
26
- expectType<FastifyInstance>(server)
27
- expectType<ConfigManager<PlatformaticService>>(server.platformatic.configManager)
28
- expectType<PlatformaticService>(server.platformatic.configManager.current)
29
- expectType<PlatformaticService>(server.platformatic.config)
30
- expectType<OpenAPI.Document>(server.swagger())
31
- expectType<MercuriusPlugin>(server.graphql)
32
- expectType<Promise<void>>(server.restart())
33
-
34
- function buildStackable (): Stackable<PlatformaticServiceConfig> {
35
- const myApp: Stackable<PlatformaticServiceConfig> = {
36
- async app (app: FastifyInstance, opts: object): Promise<void> {
37
- await platformaticService.app(app, opts)
38
- },
39
-
40
- schema: platformaticService.schema,
41
- configType: 'myApp',
42
- configManagerConfig: {
43
- version: platformaticService.configManagerConfig.version,
44
- ...platformaticService.configManagerConfig,
45
- async transformConfig (this: ConfigManager<PlatformaticServiceConfig>) {
46
- this.current.plugins = {
47
- paths: [{
48
- path: 'my-plugin',
49
- }],
50
- }
51
- },
52
- },
53
- async upgrade (config: PlatformaticServiceConfig, version: string) {
54
- const upgrade = platformaticService.configManagerConfig.upgrade
55
- if (typeof upgrade === 'function') {
56
- return upgrade.call(this, config, version)
57
- }
58
- return config
59
- },
60
- buildStackable: () => Promise.resolve({} as StackableInterface),
61
- }
62
-
63
- // configVersion is not part of ConfigManagerConfig
64
- expectError(myApp.configManagerConfig.configVersion)
65
-
66
- await start(myApp, ['--help'])
67
-
68
- return myApp
69
- }
70
-
71
- expectType<Stackable<PlatformaticServiceConfig>>(buildStackable())
72
-
73
- const generator = new Generator()
74
- expectType<Generator>(generator)
75
-
76
- class MyGenerator extends Generator {}
77
- const myGenerator = new MyGenerator()
78
-
79
- expectType<MyGenerator>(myGenerator)
80
- expectType<BaseGenerator.BaseGeneratorConfig>(myGenerator.config)
81
-
82
- function buildStackable2 (): Stackable<PlatformaticServiceConfig> {
83
- const myApp: Stackable<PlatformaticServiceConfig> = {
84
- async app (app: FastifyInstance, opts: object): Promise<void> {
85
- await platformaticService.app(app, opts)
86
- },
87
- schema: platformaticService.schema,
88
- configType: 'myApp',
89
- configManagerConfig: {
90
- ...platformaticService.configManagerConfig,
91
- async transformConfig (this: ConfigManager<PlatformaticServiceConfig>) {
92
- this.current.plugins = {
93
- paths: [{
94
- path: 'my-plugin',
95
- }],
96
- }
97
- },
98
- },
99
- buildStackable: () => Promise.resolve({} as StackableInterface),
100
- }
101
-
102
- await start(myApp, ['--help'])
103
-
104
- return myApp
105
- }
106
-
107
- expectType<Stackable<PlatformaticServiceConfig>>(buildStackable2())
package/lib/create.mjs DELETED
@@ -1,85 +0,0 @@
1
- 'use strict'
2
- import minimist from 'minimist'
3
- import { Generator } from '../lib/generator/service-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 Service 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
- { config: 'Sample Plugin and Tests', value: args.plugin },
20
- ]
21
-
22
- const p = new Table({
23
- columns: [
24
- { name: 'config', alignment: 'right' },
25
- { name: 'value', alignment: 'left' },
26
- ],
27
- })
28
-
29
- p.addRows(table)
30
- p.printTable()
31
- }
32
- async function createService (_args) {
33
- const stream = pinoPretty({
34
- translateTime: 'SYS:HH:MM:ss',
35
- ignore: 'hostname,pid',
36
- minimumLevel: 'debug',
37
- sync: true,
38
- })
39
-
40
- const logger = pino(stream)
41
-
42
- const args = minimist(process.argv.slice(2), {
43
- string: ['dir', 'port', 'hostname'],
44
- boolean: ['typescript', 'install', 'plugin', 'git'],
45
- default: {
46
- dir: join(process.cwd(), 'platformatic-service'),
47
- port: 3042,
48
- hostname: '0.0.0.0',
49
- plugin: true,
50
- typescript: false,
51
- git: false,
52
- install: true,
53
- },
54
-
55
- })
56
-
57
- printAppSummary(args, logger)
58
-
59
- const gen = new Generator({})
60
- gen.setConfig({
61
- port: args.port,
62
- hostname: args.hostname,
63
- plugin: args.plugin,
64
- tests: args.plugin,
65
- typescript: args.typescript,
66
- initGitRepository: args.git,
67
- targetDirectory: args.dir,
68
- })
69
-
70
- try {
71
- await gen.run()
72
- if (args.install) {
73
- const pkgManager = getPkgManager()
74
- const spinner = ora('Installing dependencies...').start()
75
- await execa(pkgManager, ['install'], { cwd: args.dir })
76
- spinner.succeed()
77
- }
78
-
79
- logger.info('Done! 🎉')
80
- } catch (err) {
81
- logger.error(err.message)
82
- }
83
- }
84
-
85
- export { createService }
package/lib/gen-schema.js DELETED
@@ -1,15 +0,0 @@
1
- 'use strict'
2
-
3
- const { writeFile } = require('fs/promises')
4
- const { schema: platformaticServiceSchema } = require('./schema.js')
5
-
6
- const filenameConfigJsonSchema = 'platformatic.service.schema.json'
7
-
8
- async function generateJsonSchemaConfig () {
9
- await writeFile(filenameConfigJsonSchema, JSON.stringify(platformaticServiceSchema, null, 2))
10
- }
11
-
12
- module.exports = {
13
- generateJsonSchemaConfig,
14
- filenameConfigJsonSchema,
15
- }
package/lib/gen-types.mjs DELETED
@@ -1,38 +0,0 @@
1
- import { join, dirname } from 'path'
2
- import { createRequire } from 'module'
3
- import { writeFile } from 'fs/promises'
4
- import pino from 'pino'
5
- import pretty from 'pino-pretty'
6
- import { loadConfig } from '@platformatic/config'
7
- import platformaticService from '../index.js'
8
- import { checkForDependencies } from '@platformatic/utils'
9
-
10
- const GLOBAL_TYPES_TEMPLATE = `
11
- import { FastifyInstance } from 'fastify'
12
- import { PlatformaticApp, PlatformaticServiceConfig } from '@platformatic/service'
13
-
14
- declare module 'fastify' {
15
- interface FastifyInstance {
16
- platformatic: PlatformaticApp<PlatformaticServiceConfig>
17
- }
18
- }
19
- `
20
- async function execute ({ logger, configManager }) {
21
- const fileNameOrThen = join(dirname(configManager.fullPath), 'global.d.ts')
22
- await writeFile(fileNameOrThen, GLOBAL_TYPES_TEMPLATE)
23
- }
24
-
25
- async function generateTypes (_args) {
26
- const logger = pino(pretty({
27
- translateTime: 'SYS:HH:MM:ss',
28
- ignore: 'hostname,pid',
29
- }))
30
- const { configManager, args } = await loadConfig({}, _args, platformaticService)
31
- await configManager.parseAndValidate()
32
- const config = configManager.current
33
-
34
- await execute({ logger, configManager })
35
- await checkForDependencies(logger, args, createRequire(import.meta.url), config, ['@platformatic/service'])
36
- }
37
-
38
- export { execute, generateTypes }
@@ -1,31 +0,0 @@
1
- # Platformatic Service API
2
-
3
- This is a generated [Platformatic Service](https://docs.platformatic.dev/docs/service/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
- 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 DB server is running at http://localhost:3042/
28
- - 📔 View the REST API's Swagger documentation at http://localhost:3042/documentation/
29
- - 🔍 Try out the GraphiQL web UI at http://localhost:3042/graphiql
30
-
31
-
@@ -1,11 +0,0 @@
1
- import { BaseGenerator } from '@platformatic/generators'
2
-
3
- interface KeyValue {
4
- [key: string]: string
5
- }
6
-
7
- export namespace ServiceGenerator {
8
- export class ServiceGenerator extends BaseGenerator.BaseGenerator {
9
- constructor (opts?: Omit<BaseGenerator.BaseGeneratorOptions, 'module'>)
10
- }
11
- }
@@ -1,126 +0,0 @@
1
- 'use strict'
2
-
3
- const { BaseGenerator } = require('@platformatic/generators')
4
- const { readFile } = require('node:fs/promises')
5
- const { join } = require('node:path')
6
-
7
- class ServiceGenerator extends BaseGenerator {
8
- constructor (opts = {}) {
9
- super({
10
- ...opts,
11
- module: '@platformatic/service',
12
- })
13
- }
14
-
15
- getDefaultConfig () {
16
- const config = super.getDefaultConfig()
17
- config.plugin = true
18
- config.tests = true
19
- return config
20
- }
21
-
22
- async _beforePrepare () {
23
- // if we are NOT updating, create env and files, otherwise leave as it is
24
- if (!this.config.isUpdating) {
25
- if (!this.config.isRuntimeContext) {
26
- this.addEnvVars({
27
- PLT_SERVER_HOSTNAME: this.config.hostname,
28
- PLT_SERVER_LOGGER_LEVEL: 'info',
29
- PORT: 3042,
30
- }, { overwrite: false })
31
- }
32
-
33
- this.addEnvVars({
34
- PLT_TYPESCRIPT: this.config.typescript,
35
- }, { overwrite: false, default: true })
36
-
37
- this.config.dependencies = {
38
- '@platformatic/service': `^${this.platformaticVersion}`,
39
- }
40
- }
41
- }
42
-
43
- getConfigFieldsDefinitions () {
44
- if (this.config.isRuntimeContext) {
45
- return []
46
- }
47
- return [
48
- {
49
- var: 'PLT_SERVER_HOSTNAME',
50
- label: 'What is the hostname?',
51
- default: '0.0.0.0',
52
- type: 'string',
53
- configValue: 'hostname',
54
- },
55
- {
56
- var: 'PLT_SERVER_LOGGER_LEVEL',
57
- label: 'What is the logger level?',
58
- default: 'info',
59
- type: 'string',
60
- configValue: '',
61
- },
62
- {
63
- label: 'Which port do you want to use?',
64
- var: 'PORT',
65
- default: 3042,
66
- type: 'number',
67
- configValue: 'port',
68
- },
69
- ]
70
- }
71
-
72
- async _afterPrepare () {
73
- // if we are NOT updating, create env and files, otherwise leave as it is
74
- if (!this.config.isUpdating) {
75
- const GLOBAL_TYPES_TEMPLATE = `
76
- import { FastifyInstance } from 'fastify'
77
- import { PlatformaticApp, PlatformaticServiceConfig } from '@platformatic/service'
78
-
79
- declare module 'fastify' {
80
- interface FastifyInstance {
81
- platformatic: PlatformaticApp<PlatformaticServiceConfig>
82
- }
83
- }
84
- `
85
- this.addFile({ path: '', file: 'global.d.ts', contents: GLOBAL_TYPES_TEMPLATE })
86
- this.addFile({ path: '', file: 'README.md', contents: await readFile(join(__dirname, 'README.md'), 'utf-8') })
87
- }
88
- }
89
-
90
- async _getConfigFileContents () {
91
- const { isRuntimeContext } = this.config
92
- const version = this.platformaticVersion
93
- const config = {
94
- $schema: `https://schemas.platformatic.dev/@platformatic/service/${version}.json`,
95
- service: {
96
- openapi: true,
97
- },
98
- watch: true,
99
- }
100
- if (this.config.plugin) {
101
- config.plugins = {
102
- paths: [
103
- { path: './plugins', encapsulate: false },
104
- './routes',
105
- ],
106
- typescript: `{${this.getEnvVarName('PLT_TYPESCRIPT')}}`,
107
- }
108
- }
109
-
110
- if (!isRuntimeContext) {
111
- config.server = {
112
- hostname: '{PLT_SERVER_HOSTNAME}',
113
- port: '{PORT}',
114
- logger: {
115
- level: '{PLT_SERVER_LOGGER_LEVEL}',
116
- },
117
- }
118
- }
119
-
120
- return config
121
- }
122
- }
123
-
124
- module.exports = ServiceGenerator
125
- module.exports.ServiceGenerator = ServiceGenerator
126
- module.exports.Generator = ServiceGenerator