Create a Fastify plugin that exposes a client for a remote OpenAPI or GraphQL API.
To create a client for a remote OpenAPI API, you can use the following command:
```bash
$ massimo http://example.com/to/schema/file -n myclient
```
To create a client for a remote Graphql API, you can use the following command:
```bash
$ massimo http://example.com/graphql -n myclient
```
Instead of an URL, you can also use a local file:
```bash
$ massimo path/to/schema -n myclient
```
All the above commands will create a Fastify plugin that exposes a client in the `request` object for the remote API in a folder `myclient` and a file named myclient.js inside it.
If platformatic config file is specified, it will be edited and a `clients` section will be added.
Then, in any part of your Platformatic application, you can use the client.
You can use the client in your application in Javascript, calling a GraphQL endpoint:
```js
module.exports = async function (app, opts) {
app.post('/', async (request, reply) => {
const res = await request.myclient.graphql({
query: 'query { hello }'
})
return res
})
}
```
or in Typescript, calling an OpenAPI endpoint:
```ts
import { type FastifyInstance } from 'fastify'
///
export default async function (app: FastifyInstance) {
app.get('/', async (request, reply) => {
return request.myclient.get({})
})
}
```
You can generate only the types with the --types-only flag.
```bash
$ massimo http://exmaple.com/to/schema/file --name myclient --types-only
```
Will create the single myclient.d.ts file (or .d.mts/.d.cts depending on module format and --type-extension option).
Options:
* `-c, --config ` - Path to the configuration file.
* `-n, --name ` - Name of the client.
* `-f, --folder ` - Name of the plugin folder, defaults to --name value.
* `-t, --typescript` - Generate the client plugin in TypeScript.
* `--frontend` - Generated a browser-compatible client that uses `fetch`
* `--full-response` - Client will return full response object rather than just the body.
* `--full-request` - Client will be called with all parameters wrapped in `body`, `headers` and `query` properties. Ignored if `--frontend`
* `--full` - Enables both `--full-request` and `--full-response` overriding them.
* `--optional-headers ` - Comma separated string of headers that will be marked as optional in the type file. Ignored if `--frontend`
* `--validate-response` - If set, will validate the response body against the schema. Ignored if `--frontend`
* `--language js|ts` - Generate a Javascript or Typescript frontend client. Only works if `--frontend`
* `--url-auth-headers ` - When the Open API schema is passed as URL (instead of static file) this property allow to pass authorization headers. Headers should be passed as `string` (e.g. `'{"authorization":"42"}'`).
* `--types-only` - Generate only the type file.
* `--types-comment` - Add a comment at the beginning of the auto generated `.d.ts` type definition.
* `--with-credentials` - Adds "credentials: 'include'" to all fetch requests (only for frontend clients).
* `--props-optional` - If `true`, properties will be defined as optional unless they're part of the `required` array. By default this option is `true`.
* `--retry-timeout-ms` - If passed, the HTTP request to get an Open API schema will be retried (reference: https://undici.nodejs.org/#/docs/api/RetryHandler.md)
* `--type-extension` - Force the use of module-specific type extensions (.d.mts for ESM, .d.cts for CJS) instead of the default logic.
* `--skip-prefixed-url` - If passed, it will avoid unnecessary extra HTTP call to check if, for instance, the `/documentation/json` Open API URL exists