Generate types and API client functionality from a api.json
file served by your registry.
Install @pentops/jsonapi-jdef-ts-generator
as a dev dependency and @pentops/jsonapi-request
as a dependency (if you are going to use the generated API client).
Create a .jdef_config.js
file in your project root. This file should default export a configuration object:
const EXCLUDED_NAMESPACES = ['service', 'topic'];
export default {
typeOutput: {
fileName: 'index.ts',
directory: './packages/types/generated',
importPath: '@yourcompany/types',
},
clientOutput: {
fileName: 'index.ts',
directory: './packages/api-client/generated',
},
client: {
// Remove the excluded namespaces from the method name and camelCase the result
methodNameWriter: (method) =>
method.fullGrpcName
.split(/[./]/)
.filter((s) => s && !EXCLUDED_NAMESPACES.includes(s.toLowerCase()))
.map((s, i) => (i === 0 ? s : s[0].toUpperCase() + s.slice(1)))
.join(''),
},
types: {
enumType: 'enum',
// Remove the excluded namespaces from the interface/enum name and camelCase the result
nameWriter: (x) =>
x
.split('.')
.filter((s) => s && !EXCLUDED_NAMESPACES.includes(s.toLowerCase()))
.map((s) => s?.[0]?.toUpperCase() + s?.slice(1))
.join(''),
},
jsonSource: {
path: 'api.json',
},
};
You can specify the source of the api.json file using the jsonSource
property.
- The
path
property should be the path to a localapi.json
file. - The
service
property should be set for a remoteapi.json
file. It should be an object with the following properties:-
url
: The URL of the remoteapi.json
file. -
auth
: An optional object containing atoken
if required.
-
See the configuration definitions for more information.
Add a script to your package.json
to run the generator.
{
"scripts": {
"generate-types": "jdef-ts-generator"
}
}
You will need to have @pentops/jsonapi-request
installed as a dependency if you're going to use the generated API client.