@groton/canvas-api.swagger-renderer 0.1.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.
- package/CHANGELOG.md +59 -0
- package/LICENSE +674 -0
- package/README.md +117 -0
- package/bin/swagger-renderer +2 -0
- package/dist/Download.d.ts +13 -0
- package/dist/Download.js +78 -0
- package/dist/Render/Annotation.d.ts +46 -0
- package/dist/Render/Annotation.js +1 -0
- package/dist/Render/Models.d.ts +14 -0
- package/dist/Render/Models.js +135 -0
- package/dist/Render/Operations.d.ts +16 -0
- package/dist/Render/Operations.js +234 -0
- package/dist/Render/Overrides.d.ts +16 -0
- package/dist/Render/Overrides.js +138 -0
- package/dist/Render/Render.d.ts +19 -0
- package/dist/Render/Render.js +124 -0
- package/dist/Render/TSAnnotation.d.ts +19 -0
- package/dist/Render/TSAnnotation.js +1 -0
- package/dist/Render/TypeScript.d.ts +10 -0
- package/dist/Render/TypeScript.js +84 -0
- package/dist/Render/importPath.d.ts +2 -0
- package/dist/Render/importPath.js +9 -0
- package/dist/Render/index.d.ts +1 -0
- package/dist/Render/index.js +5 -0
- package/dist/Render/writePrettier.d.ts +2 -0
- package/dist/Render/writePrettier.js +25 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -0
- package/package.json +53 -0
package/README.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# @groton/canvas-api.swagger-renderer
|
|
2
|
+
|
|
3
|
+
Render Canvas LMS Swagger 1.0 API documentation as TypeScript client
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@groton/canvas-api.swagger-renderer)
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @groton/canvas-api.swagger-renderer
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
See all options:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
swagger-renderer --help
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Download API documentation (to `./spec/` by default):
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
swagger-renderer download --instanceUrl "https://example.instructure.com"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Render downloaded spec (from `./spec/` and to `./src/Resources/` and `./src/Endpoints/` by default):
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
swagger-renderer render
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
See [@groton/canvas-api's scripts](https://github.com/groton-school/canvas-cli/blob/main/packages/api/canvas-api/package.json) for an example use.
|
|
34
|
+
|
|
35
|
+
## Annotation
|
|
36
|
+
|
|
37
|
+
`swagger-renderer` analyzes the provided Swagger specification and annotates it before using those annotations to render the actual TypeScript source code. The annotations can be output to `./src/map.json` as part of the rendering process.
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
swagger-renderer render --map
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The annotation types are defined in [./src/Render/Annotation.ts](https://github.com/groton-school/canvas-cli/blob/main/packages/api/swagger-renderer/src/Render/Annotation.ts) and [./src/Render/TSAnnotation.ts](https://github.com/groton-school/canvas-cli/blob/main/packages/api/swagger-renderer/src/Render/TSAnnotation.ts). These annotation types are useful to understand when defining overrides.
|
|
44
|
+
|
|
45
|
+
## Overrides
|
|
46
|
+
|
|
47
|
+
Due to eccentricities in the documentation of the Canvas LMS API, it is desireable to override the automated specification in a number of respects. See [@groton/canvas-api's known issues](https://github.com/groton-school/canvas-cli/tree/main/packages/api/canvas-api#known-issues) for discussion of a real-world example.
|
|
48
|
+
|
|
49
|
+
Overrides for `swagger-renderer` are defined in a JSON file. The object has three main properties.
|
|
50
|
+
|
|
51
|
+
### `tsReferences`
|
|
52
|
+
|
|
53
|
+
These provide specific reference information for otherwise ambiguous types. Usually the reference information follows the pattern:
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
{
|
|
57
|
+
"tsReferences": [
|
|
58
|
+
{
|
|
59
|
+
"type": "Account",
|
|
60
|
+
"filePath": "Resources/Accounts.ts"
|
|
61
|
+
}
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### `tsTypes`
|
|
67
|
+
|
|
68
|
+
The `tsTypes` property is a hash of type values provided in the Swagger specification to TypeScript type definitions (defined using the [annotation](#annotation) syntax).
|
|
69
|
+
|
|
70
|
+
```json
|
|
71
|
+
{
|
|
72
|
+
"tsTypes": {
|
|
73
|
+
"Positive Integer": {
|
|
74
|
+
"type": "number"
|
|
75
|
+
},
|
|
76
|
+
"Integer": {
|
|
77
|
+
"type": "number"
|
|
78
|
+
},
|
|
79
|
+
"DateTime": {
|
|
80
|
+
"type": "string",
|
|
81
|
+
"description": "format: date-time"
|
|
82
|
+
},
|
|
83
|
+
"object": {
|
|
84
|
+
"type": "JSONObject",
|
|
85
|
+
"tsReferences": [
|
|
86
|
+
{
|
|
87
|
+
"type": "JSONObject",
|
|
88
|
+
"packagePath": "@battis/typescript-tricks"
|
|
89
|
+
}
|
|
90
|
+
]
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### `operations`
|
|
97
|
+
|
|
98
|
+
The `operations` property is a hash of Swagger specification operation nicknames and [`Partial<AnnotatedOperation>`](https://github.com/groton-school/canvas-cli/blob/main/packages/api/swagger-renderer/src/Render/Annotation.ts) definitions to supplement or replace missing or deficient documentation.
|
|
99
|
+
|
|
100
|
+
```json
|
|
101
|
+
{
|
|
102
|
+
"operations": {
|
|
103
|
+
"edit_assignment": {
|
|
104
|
+
"tsFormParameters": [
|
|
105
|
+
{
|
|
106
|
+
"tsName": "\"assignment[submission_types]\"",
|
|
107
|
+
"description": "Only applies if the assignment doesn't have student submissions.\n\nList of supported submission types for the assignment.\nUnless the assignment is allowing online submissions, the array should\nonly have one element.\n\nIf not allowing online submissions, your options are:\n \"online_quiz\"\n \"none\"\n \"on_paper\"\n \"discussion_topic\"\n \"external_tool\"\n\nIf you are allowing online submissions, you can have one or many\nallowed submission types:\n\n \"online_upload\"\n \"online_text_entry\"\n \"online_url\"\n \"media_recording\" (Only valid when the Kaltura plugin is enabled)\n \"student_annotation\"",
|
|
108
|
+
"tsOptional": "?",
|
|
109
|
+
"tsType": {
|
|
110
|
+
"type": "('online_quiz'|'none'|'on_paper'|'discussion_topic'|'external_tool'|'online_upload'|'online_text_entry'|'online_url'|'media_recording'|'student_annotation')[]"
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
]
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PathString, URLString } from '@battis/descriptive-types';
|
|
2
|
+
import * as Plugin from '@battis/qui-cli.plugin';
|
|
3
|
+
type Configuration = Plugin.Configuration & {
|
|
4
|
+
instanceUrl?: URLString;
|
|
5
|
+
specPath?: PathString;
|
|
6
|
+
};
|
|
7
|
+
export declare const name = "download";
|
|
8
|
+
export declare const src: string;
|
|
9
|
+
export declare function configure(config?: Configuration): void;
|
|
10
|
+
export declare function options(): Plugin.Options;
|
|
11
|
+
export declare function init(args: Plugin.ExpectedArguments<typeof options>): void;
|
|
12
|
+
export declare function run(): Promise<string[]>;
|
|
13
|
+
export {};
|
package/dist/Download.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Colors } from '@battis/qui-cli.colors';
|
|
2
|
+
import { Log } from '@battis/qui-cli.log';
|
|
3
|
+
import * as Plugin from '@battis/qui-cli.plugin';
|
|
4
|
+
import { Root } from '@battis/qui-cli.root';
|
|
5
|
+
import fetch from 'node-fetch';
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import ora from 'ora';
|
|
9
|
+
import PQueue from 'p-queue';
|
|
10
|
+
Root.configure({ root: process.cwd() });
|
|
11
|
+
export const name = 'download';
|
|
12
|
+
export const src = import.meta.dirname;
|
|
13
|
+
let instanceUrl = 'https://canvas.instructure.com';
|
|
14
|
+
let specPath = './spec';
|
|
15
|
+
export function configure(config = {}) {
|
|
16
|
+
instanceUrl = Plugin.hydrate(config.instanceUrl, instanceUrl);
|
|
17
|
+
specPath = Plugin.hydrate(config.specPath, specPath);
|
|
18
|
+
}
|
|
19
|
+
export function options() {
|
|
20
|
+
return {
|
|
21
|
+
opt: {
|
|
22
|
+
instanceUrl: {
|
|
23
|
+
description: `URL of the Canvas instance from which to download the Swagger API spec (default: ${Colors.url(instanceUrl)})`,
|
|
24
|
+
default: instanceUrl
|
|
25
|
+
},
|
|
26
|
+
specPath: {
|
|
27
|
+
description: `Path to store the downloaded spec files (default: ${Colors.url(specPath)})`,
|
|
28
|
+
default: specPath
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export function init(args) {
|
|
34
|
+
configure(args.values);
|
|
35
|
+
}
|
|
36
|
+
export async function run() {
|
|
37
|
+
const spinner = ora(`Downloading Swagger API definition`).start();
|
|
38
|
+
const queue = new PQueue({ interval: 1000 });
|
|
39
|
+
instanceUrl = path.join(instanceUrl, 'doc/api');
|
|
40
|
+
specPath = path.resolve(Root.path(), specPath);
|
|
41
|
+
if (!fs.existsSync(specPath)) {
|
|
42
|
+
fs.mkdirSync(specPath, { recursive: true });
|
|
43
|
+
}
|
|
44
|
+
const paths = ['/api-docs.json'];
|
|
45
|
+
const result = [];
|
|
46
|
+
do {
|
|
47
|
+
await queue.add(async () => {
|
|
48
|
+
const url = new URL(instanceUrl + paths.pop());
|
|
49
|
+
spinner.text = Colors.url(url);
|
|
50
|
+
const response = await fetch(url);
|
|
51
|
+
if (response.ok) {
|
|
52
|
+
const text = await response.text();
|
|
53
|
+
try {
|
|
54
|
+
const swagger = JSON.parse(text);
|
|
55
|
+
const filePath = path.join(specPath, path.basename(url.toString()));
|
|
56
|
+
fs.writeFileSync(filePath, text);
|
|
57
|
+
result.push(filePath);
|
|
58
|
+
if (swagger.apis) {
|
|
59
|
+
swagger.apis.map((api) => {
|
|
60
|
+
if (api.path.endsWith('.json')) {
|
|
61
|
+
paths.push(api.path);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
spinner.succeed(url.toString());
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
spinner.fail(`${Colors.url(url)}: ${Colors.error(error)}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
spinner.fail(`${Colors.url(url)}: ${Colors.error(`${response.status} ${response.statusText}`)}`);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
} while (paths.length);
|
|
76
|
+
Log.info(`Spec files written to ${Colors.url(specPath)}`);
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { PathString } from '@battis/descriptive-types';
|
|
2
|
+
import * as Swagger from '@groton/swagger-spec-ts';
|
|
3
|
+
import { TSDeprecation, TSExport, TSName, TSReference, TSType } from './TSAnnotation.js';
|
|
4
|
+
export type AnnotatedApiObject = Swagger.v1p2.ApiObject & {
|
|
5
|
+
operations: AnnotatedOperation[];
|
|
6
|
+
};
|
|
7
|
+
export type AnnotatedOperation = Swagger.v1p2.OperationObject & {
|
|
8
|
+
specPath: PathString;
|
|
9
|
+
tsFilePath: PathString;
|
|
10
|
+
tsImports?: TSReference[];
|
|
11
|
+
tsEndpoint?: PathString;
|
|
12
|
+
tsName: TSName;
|
|
13
|
+
tsType: TSType;
|
|
14
|
+
tsPathParameters?: (AnnotatedParameter & {
|
|
15
|
+
paramType: 'path';
|
|
16
|
+
})[];
|
|
17
|
+
tsQueryParameters?: (AnnotatedParameter & {
|
|
18
|
+
paramType: 'query';
|
|
19
|
+
})[];
|
|
20
|
+
tsBodyParameters?: (AnnotatedParameter & {
|
|
21
|
+
paramType: 'body';
|
|
22
|
+
})[];
|
|
23
|
+
tsFormParameters?: (AnnotatedParameter & {
|
|
24
|
+
paramType: 'form';
|
|
25
|
+
})[];
|
|
26
|
+
tsPaginated?: boolean;
|
|
27
|
+
tsUpload?: boolean;
|
|
28
|
+
};
|
|
29
|
+
export type AnnotatedParameter = Swagger.v1p2.ParameterObject & {
|
|
30
|
+
tsDeprecation?: TSDeprecation;
|
|
31
|
+
tsName: TSName;
|
|
32
|
+
tsType: TSType;
|
|
33
|
+
};
|
|
34
|
+
export type AnnotatedModel = Omit<Swagger.v1p2.ModelsObject, 'properties'> & {
|
|
35
|
+
specPath: PathString;
|
|
36
|
+
tsImports?: TSReference[];
|
|
37
|
+
tsDeprecation?: TSDeprecation;
|
|
38
|
+
tsExport?: TSExport;
|
|
39
|
+
tsName: TSName;
|
|
40
|
+
properties: AnnotatedProperty[];
|
|
41
|
+
};
|
|
42
|
+
export type AnnotatedProperty = Swagger.v1p2.DataTypeBase & {
|
|
43
|
+
tsDeprecation?: TSDeprecation;
|
|
44
|
+
tsName: TSName;
|
|
45
|
+
tsType: TSType;
|
|
46
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { PathString } from '@battis/descriptive-types';
|
|
2
|
+
import * as Swagger from '@groton/swagger-spec-ts';
|
|
3
|
+
import { AnnotatedModel } from './Annotation.js';
|
|
4
|
+
type GenerateOptions = {
|
|
5
|
+
specPaths: PathString[];
|
|
6
|
+
templatePath: PathString;
|
|
7
|
+
outputPath: PathString;
|
|
8
|
+
};
|
|
9
|
+
export type Annotation = {
|
|
10
|
+
spec: Record<PathString, Swagger.v1p2.ApiDeclaration[]>;
|
|
11
|
+
models: Record<PathString, AnnotatedModel[]>;
|
|
12
|
+
};
|
|
13
|
+
export declare function generate(options: GenerateOptions): Promise<Annotation>;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import Handlebars from 'handlebars';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { importPath } from './importPath.js';
|
|
5
|
+
import * as Overrides from './Overrides.js';
|
|
6
|
+
import { toTSDeprecation, toTSExport, toTSNamespace, toTSPropertyName, toTSType, toTSTypeName } from './TypeScript.js';
|
|
7
|
+
import { writePrettier } from './writePrettier.js';
|
|
8
|
+
export async function generate(options) {
|
|
9
|
+
const annotations = annotateFileList(options);
|
|
10
|
+
annotateImports(annotations);
|
|
11
|
+
const outputOptions = {
|
|
12
|
+
...options,
|
|
13
|
+
...annotations
|
|
14
|
+
};
|
|
15
|
+
await outputModels(outputOptions);
|
|
16
|
+
await outputModelIndex(outputOptions);
|
|
17
|
+
return annotations;
|
|
18
|
+
}
|
|
19
|
+
function annotateFileList({ outputPath, specPaths }) {
|
|
20
|
+
const annotation = { spec: {}, models: {} };
|
|
21
|
+
for (const specPath of specPaths) {
|
|
22
|
+
const annotatedSpec = annotateJSONFile({ specPath, outputPath });
|
|
23
|
+
annotation.spec = { ...annotation.spec, ...annotatedSpec.spec };
|
|
24
|
+
annotation.models = { ...annotation.models, ...annotatedSpec.models };
|
|
25
|
+
}
|
|
26
|
+
return annotation;
|
|
27
|
+
}
|
|
28
|
+
function annotateJSONFile({ specPath, outputPath }) {
|
|
29
|
+
const spec = JSON.parse(fs.readFileSync(specPath).toString());
|
|
30
|
+
return {
|
|
31
|
+
spec: { [specPath]: [spec] },
|
|
32
|
+
models: {
|
|
33
|
+
[toFilePath(outputPath, specPath)]: annotateModels(spec, specPath)
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function annotateModels(api, specPath) {
|
|
38
|
+
const models = [];
|
|
39
|
+
for (const modelId in api.models) {
|
|
40
|
+
const model = api.models[modelId];
|
|
41
|
+
const properties = [];
|
|
42
|
+
const tsImports = [];
|
|
43
|
+
for (const propertyId in model.properties) {
|
|
44
|
+
const property = model.properties[propertyId];
|
|
45
|
+
const annotatedProperty = {
|
|
46
|
+
...property,
|
|
47
|
+
tsDeprecation: toTSDeprecation(property),
|
|
48
|
+
tsName: toTSPropertyName(propertyId),
|
|
49
|
+
tsType: toTSType(property)
|
|
50
|
+
};
|
|
51
|
+
properties.push(annotatedProperty);
|
|
52
|
+
if (annotatedProperty.tsType.tsReferences) {
|
|
53
|
+
tsImports.push(...annotatedProperty.tsType.tsReferences);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const annotatedModel = {
|
|
57
|
+
specPath,
|
|
58
|
+
...model,
|
|
59
|
+
tsImports,
|
|
60
|
+
tsDeprecation: toTSDeprecation(model),
|
|
61
|
+
tsName: toTSTypeName(modelId),
|
|
62
|
+
properties
|
|
63
|
+
};
|
|
64
|
+
annotatedModel.tsExport = toTSExport(annotatedModel);
|
|
65
|
+
/*
|
|
66
|
+
* TODO force IDs to strings
|
|
67
|
+
* https://developerdocs.instructure.com/services/canvas#schema
|
|
68
|
+
*/
|
|
69
|
+
models.push(annotatedModel);
|
|
70
|
+
}
|
|
71
|
+
return models.map((model) => {
|
|
72
|
+
model.tsImports = model.tsImports?.filter((tsType) => !models.find((model) => model.tsName === tsType.type));
|
|
73
|
+
return model;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function annotateImports(annotation) {
|
|
77
|
+
for (const filePath in annotation.models) {
|
|
78
|
+
for (const model of annotation.models[filePath]) {
|
|
79
|
+
for (const tsImport of model.tsImports || []) {
|
|
80
|
+
tsImport.filePath =
|
|
81
|
+
Overrides.tsReference(tsImport.type)?.filePath ||
|
|
82
|
+
Object.keys(annotation.models).find((filePath) => annotation.models[filePath]
|
|
83
|
+
.map((model) => model.tsName)
|
|
84
|
+
.includes(tsImport.type));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async function outputModels({ templatePath, outputPath, models }) {
|
|
90
|
+
const template = Handlebars.compile(fs.readFileSync(path.join(templatePath, 'Model.handlebars')).toString());
|
|
91
|
+
fs.mkdirSync(outputPath, { recursive: true });
|
|
92
|
+
for (const filePath in models) {
|
|
93
|
+
if (models[filePath].length) {
|
|
94
|
+
await writePrettier(filePath, template({
|
|
95
|
+
models: models[filePath],
|
|
96
|
+
tsImports: models[filePath]
|
|
97
|
+
.reduce((tsImports, model) => {
|
|
98
|
+
for (const tsImport of model.tsImports || []) {
|
|
99
|
+
if (!tsImports.find((t) => t.type === tsImport.type)) {
|
|
100
|
+
tsImports.push(tsImport);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return tsImports;
|
|
104
|
+
}, [])
|
|
105
|
+
.map((tsImport) => {
|
|
106
|
+
if (tsImport.filePath) {
|
|
107
|
+
return {
|
|
108
|
+
...tsImport,
|
|
109
|
+
filePath: importPath(filePath, tsImport.filePath)
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
return tsImport;
|
|
113
|
+
})
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
async function outputModelIndex({ templatePath, outputPath, models }) {
|
|
119
|
+
const template = Handlebars.compile(fs.readFileSync(path.join(templatePath, 'ModelIndex.handlebars')).toString());
|
|
120
|
+
await writePrettier(path.join(outputPath, 'index.ts'), template({
|
|
121
|
+
index: Object.keys(models)
|
|
122
|
+
.filter((filePath) => models[filePath].length)
|
|
123
|
+
.map((filePath) => ({
|
|
124
|
+
tsNamespace: toTSNamespace(filePath),
|
|
125
|
+
filePath: importPath(path.join(outputPath, 'index.ts'), filePath)
|
|
126
|
+
}))
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
function toFilePath(outputPath, specPath) {
|
|
130
|
+
return path.join(outputPath, path
|
|
131
|
+
.basename(specPath, '.json')
|
|
132
|
+
.split('_')
|
|
133
|
+
.map((token) => token[0].toUpperCase() + token.slice(1))
|
|
134
|
+
.join('') + '.ts');
|
|
135
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { PathString } from '@battis/descriptive-types';
|
|
2
|
+
import { AnnotatedOperation } from './Annotation.js';
|
|
3
|
+
import * as Models from './Models.js';
|
|
4
|
+
type GenerateOptions = Models.Annotation & {
|
|
5
|
+
outputPath: PathString;
|
|
6
|
+
templatePath: PathString;
|
|
7
|
+
};
|
|
8
|
+
type AnnotateOptions = Models.Annotation & {
|
|
9
|
+
outputPath: PathString;
|
|
10
|
+
};
|
|
11
|
+
type Annotation = Models.Annotation & {
|
|
12
|
+
operations: Record<PathString, AnnotatedOperation>;
|
|
13
|
+
};
|
|
14
|
+
export declare function generate({ spec, models, outputPath, templatePath }: GenerateOptions): Promise<Annotation>;
|
|
15
|
+
export declare function annotateOperations({ outputPath, ...annotation }: AnnotateOptions): Annotation;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import Handlebars from 'handlebars';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { importPath } from './importPath.js';
|
|
5
|
+
import * as Overrides from './Overrides.js';
|
|
6
|
+
import { toTSDeprecation, toTSNamespace, toTSPropertyName, toTSType } from './TypeScript.js';
|
|
7
|
+
import { writePrettier } from './writePrettier.js';
|
|
8
|
+
export async function generate({ spec, models, outputPath, templatePath }) {
|
|
9
|
+
const annotation = annotateOperations({ spec, models, outputPath });
|
|
10
|
+
annotateImports(annotation);
|
|
11
|
+
await outputOperations({ ...annotation, outputPath, templatePath });
|
|
12
|
+
await outputOperationIndices(outputPath, templatePath);
|
|
13
|
+
return annotation;
|
|
14
|
+
}
|
|
15
|
+
export function annotateOperations({ outputPath, ...annotation }) {
|
|
16
|
+
const clientReference = {
|
|
17
|
+
type: 'client',
|
|
18
|
+
filePath: path.join(outputPath, '../Client.ts')
|
|
19
|
+
};
|
|
20
|
+
const operations = {};
|
|
21
|
+
for (const specPath in annotation.spec) {
|
|
22
|
+
for (const spec of annotation.spec[specPath]) {
|
|
23
|
+
for (const endpoint of spec.apis) {
|
|
24
|
+
for (const operation of endpoint.operations || []) {
|
|
25
|
+
const tsImports = [{ ...clientReference }];
|
|
26
|
+
const tsName = toTSMethodName(operation);
|
|
27
|
+
let tsType = toTSType(operation);
|
|
28
|
+
if (tsType.type === 'unknown' && operation.type) {
|
|
29
|
+
tsType = {
|
|
30
|
+
type: operation.type,
|
|
31
|
+
tsReferences: [{ type: operation.type }]
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
if (tsType.tsReferences) {
|
|
35
|
+
tsImports.push(...tsType.tsReferences);
|
|
36
|
+
}
|
|
37
|
+
let tsPaginated = undefined;
|
|
38
|
+
if (tsType.type.endsWith('[]')) {
|
|
39
|
+
tsPaginated = true;
|
|
40
|
+
tsImports.push({
|
|
41
|
+
type: 'Paginated',
|
|
42
|
+
packagePath: '@groton/canvas-api.client.base'
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const tsUpload = tsName === 'upload';
|
|
46
|
+
if (tsUpload) {
|
|
47
|
+
tsImports.push({
|
|
48
|
+
type: 'FileLocation',
|
|
49
|
+
packagePath: '@groton/canvas-api.client.base'
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
// @ts-expect-error 2322 TODO $ref typing
|
|
53
|
+
const annotatedOperation = {
|
|
54
|
+
...operation,
|
|
55
|
+
specPath,
|
|
56
|
+
tsImports,
|
|
57
|
+
tsType,
|
|
58
|
+
tsEndpoint: decodeURIComponent(new URL(spec.basePath + endpoint.path).pathname),
|
|
59
|
+
tsName,
|
|
60
|
+
tsUpload,
|
|
61
|
+
tsPaginated
|
|
62
|
+
};
|
|
63
|
+
annotatedOperation.tsFilePath = path.join(outputPath, toOperationPath(endpoint.path, annotatedOperation), tsName + '.ts');
|
|
64
|
+
for (const parameter of operation.parameters) {
|
|
65
|
+
const annotatedParameter = {
|
|
66
|
+
...parameter,
|
|
67
|
+
tsDeprecation: toTSDeprecation(parameter),
|
|
68
|
+
tsName: toTSPropertyName(parameter.name),
|
|
69
|
+
tsType: toTSType(parameter)
|
|
70
|
+
};
|
|
71
|
+
if (annotatedParameter.tsType.type === 'unknown') {
|
|
72
|
+
annotatedParameter.tsType = {
|
|
73
|
+
type: parameter.type,
|
|
74
|
+
tsReferences: [{ type: parameter.type }]
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (annotatedParameter.tsType.tsReferences) {
|
|
78
|
+
annotatedOperation.tsImports.push(...annotatedParameter.tsType.tsReferences);
|
|
79
|
+
}
|
|
80
|
+
const paramType = ('ts' +
|
|
81
|
+
annotatedParameter.paramType[0].toUpperCase() +
|
|
82
|
+
annotatedParameter.paramType.slice(1) +
|
|
83
|
+
'Parameters');
|
|
84
|
+
if (!(paramType in annotatedOperation)) {
|
|
85
|
+
annotatedOperation[paramType] = [];
|
|
86
|
+
}
|
|
87
|
+
if (Array.isArray(annotatedOperation[paramType])) {
|
|
88
|
+
// @ts-expect-error 2345 TODO fix wonky annotation typing
|
|
89
|
+
annotatedOperation[paramType].push(annotatedParameter);
|
|
90
|
+
}
|
|
91
|
+
// force path params to accept numbers too
|
|
92
|
+
for (const param of annotatedOperation['tsPathParameters'] || []) {
|
|
93
|
+
if ((param.name === 'id' || /_id$/.test(param.name)) &&
|
|
94
|
+
param.tsType.type === 'string') {
|
|
95
|
+
param.tsType.description = `type: ${param.tsType.type} ${param.tsType.description || ''}`;
|
|
96
|
+
param.tsType.type = `${param.tsType.type} | number`;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
operations[annotatedOperation.tsFilePath] =
|
|
101
|
+
Overrides.operation(annotatedOperation);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return { ...annotation, operations };
|
|
107
|
+
}
|
|
108
|
+
function annotateImports({ operations, models }) {
|
|
109
|
+
for (const filePath in operations) {
|
|
110
|
+
operations[filePath].tsImports = operations[filePath].tsImports?.reduce((tsImports, tsReference) => {
|
|
111
|
+
const match = tsImports.find((t) => t.type === tsReference.type);
|
|
112
|
+
if (!match) {
|
|
113
|
+
tsImports.push(tsReference);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
if ((match.filePath && match.filePath !== tsReference.filePath) ||
|
|
117
|
+
(match.packagePath && match.packagePath !== tsReference.packagePath)) {
|
|
118
|
+
throw new TypeError(`Importing two identically named objects from different files: ${JSON.stringify({ tsReference, match })}.`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return tsImports;
|
|
122
|
+
}, []);
|
|
123
|
+
for (const tsImport of operations[filePath].tsImports || []) {
|
|
124
|
+
tsImport.filePath =
|
|
125
|
+
tsImport.filePath ||
|
|
126
|
+
// look first for definitions local to the same spec file
|
|
127
|
+
Object.keys(models).find((filePath) => models[filePath].find((model) => model.specPath === operations[filePath]?.specPath &&
|
|
128
|
+
model.tsName == tsImport.type)) ||
|
|
129
|
+
// check for overrides
|
|
130
|
+
Overrides.tsReference(tsImport.type)?.filePath ||
|
|
131
|
+
// find what's available
|
|
132
|
+
Object.keys(models).find((filePath) => models[filePath].map((model) => model.tsName).includes(tsImport.type));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function toOperationPath(endpointPath, operation) {
|
|
137
|
+
return operation.parameters
|
|
138
|
+
.reduce((tsFilePath, parameter) => {
|
|
139
|
+
if (parameter.paramType === 'path') {
|
|
140
|
+
return tsFilePath.replace(new RegExp(`{${parameter.name}}/?`), '');
|
|
141
|
+
}
|
|
142
|
+
return tsFilePath;
|
|
143
|
+
}, endpointPath)
|
|
144
|
+
.split('/')
|
|
145
|
+
.map((token) => token.length
|
|
146
|
+
? token
|
|
147
|
+
.split('_')
|
|
148
|
+
.map((t) => t[0].toUpperCase() + t.slice(1))
|
|
149
|
+
.join('')
|
|
150
|
+
: undefined)
|
|
151
|
+
.join('/');
|
|
152
|
+
}
|
|
153
|
+
function toTSMethodName(operation) {
|
|
154
|
+
switch (operation.method) {
|
|
155
|
+
case 'GET':
|
|
156
|
+
if (operation.nickname.startsWith('list_')) {
|
|
157
|
+
return 'list';
|
|
158
|
+
}
|
|
159
|
+
if (operation.nickname.startsWith('get_')) {
|
|
160
|
+
return 'get';
|
|
161
|
+
}
|
|
162
|
+
// eslint-disable-next-line no-fallthrough
|
|
163
|
+
case 'POST':
|
|
164
|
+
if (operation.nickname.startsWith('create_')) {
|
|
165
|
+
return 'create';
|
|
166
|
+
}
|
|
167
|
+
else if (operation.nickname.startsWith('upload_file')) {
|
|
168
|
+
return 'upload';
|
|
169
|
+
}
|
|
170
|
+
// eslint-disable-next-line no-fallthrough
|
|
171
|
+
case 'PUT':
|
|
172
|
+
if (operation.nickname.startsWith('update_') ||
|
|
173
|
+
operation.nickname.startsWith('edit_')) {
|
|
174
|
+
return 'update';
|
|
175
|
+
}
|
|
176
|
+
// eslint-disable-next-line no-fallthrough
|
|
177
|
+
case 'PATCH':
|
|
178
|
+
if (operation.nickname.startsWith('batch_update_')) {
|
|
179
|
+
return 'batchUpdate';
|
|
180
|
+
}
|
|
181
|
+
// eslint-disable-next-line no-fallthrough
|
|
182
|
+
default:
|
|
183
|
+
return operation.nickname;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async function outputOperations({ operations, templatePath }) {
|
|
187
|
+
const template = Handlebars.compile(fs.readFileSync(path.join(templatePath, 'Operation.handlebars')).toString());
|
|
188
|
+
for (const filePath in operations) {
|
|
189
|
+
await writePrettier(filePath, template({
|
|
190
|
+
...operations[filePath],
|
|
191
|
+
tsImports: operations[filePath].tsImports?.map((tsImport) => {
|
|
192
|
+
if (tsImport.filePath) {
|
|
193
|
+
return {
|
|
194
|
+
...tsImport,
|
|
195
|
+
filePath: importPath(filePath, tsImport.filePath)
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
return tsImport;
|
|
199
|
+
})
|
|
200
|
+
}));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
async function outputOperationIndices(outputPath, templatePath) {
|
|
204
|
+
const template = Handlebars.compile(fs
|
|
205
|
+
.readFileSync(path.join(templatePath, 'OperationIndex.handlebars'))
|
|
206
|
+
.toString());
|
|
207
|
+
async function recursiveIndex(outputPath) {
|
|
208
|
+
const modules = await Promise.all(fs
|
|
209
|
+
.readdirSync(outputPath)
|
|
210
|
+
.filter((fileName) => !fileName.startsWith('.'))
|
|
211
|
+
.map((fileName) => ({
|
|
212
|
+
tsNamespace: `as ${toTSNamespace(fileName)}`,
|
|
213
|
+
filePath: path.join(outputPath, fileName)
|
|
214
|
+
}))
|
|
215
|
+
.map(async (module) => {
|
|
216
|
+
if (fs.lstatSync(module.filePath).isDirectory()) {
|
|
217
|
+
await recursiveIndex(module.filePath);
|
|
218
|
+
module.filePath = path.join(module.filePath, 'index.ts');
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
module.tsNamespace = '';
|
|
222
|
+
}
|
|
223
|
+
return module;
|
|
224
|
+
}));
|
|
225
|
+
const filePath = path.join(outputPath, 'index.ts');
|
|
226
|
+
await writePrettier(filePath, template({
|
|
227
|
+
modules: modules.map((module) => ({
|
|
228
|
+
...module,
|
|
229
|
+
filePath: importPath(filePath, module.filePath)
|
|
230
|
+
}))
|
|
231
|
+
}));
|
|
232
|
+
}
|
|
233
|
+
await recursiveIndex(outputPath);
|
|
234
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { PathString } from '@battis/descriptive-types';
|
|
2
|
+
import { AnnotatedOperation } from './Annotation.js';
|
|
3
|
+
import { TSName, TSReference, TSType } from './TSAnnotation.js';
|
|
4
|
+
export type Collection = {
|
|
5
|
+
/** Import paths for ambiguously specified types */
|
|
6
|
+
tsReferences?: TSReference[];
|
|
7
|
+
/** Hash of non-standard type values to TSType definitions */
|
|
8
|
+
tsTypes?: Record<string, TSType>;
|
|
9
|
+
/** Hash of OperationObject.nickname to partial OperationObject definitions */
|
|
10
|
+
operations?: Record<string, Partial<AnnotatedOperation>>;
|
|
11
|
+
};
|
|
12
|
+
export declare function setOverrides(overrides: Collection): void;
|
|
13
|
+
export declare function setOutputPath(outputPath: PathString): void;
|
|
14
|
+
export declare function tsReference(type: TSName): TSReference | undefined;
|
|
15
|
+
export declare function tsType(type: string): TSType | undefined;
|
|
16
|
+
export declare function operation(operation: AnnotatedOperation): AnnotatedOperation;
|