Skip to content

Commit 82886c3

Browse files
committed
Stop using rest-api-spec altogether
1 parent 512d772 commit 82886c3

File tree

3 files changed

+29
-39
lines changed

3 files changed

+29
-39
lines changed

compiler/src/compiler.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ import { writeFile, mkdir } from 'fs/promises'
2121
import { join } from 'path'
2222
import stringify from 'safe-stable-stringify'
2323
import { Model } from './model/metamodel'
24-
import {
25-
compileEndpoints,
26-
compileSpecification
27-
} from './model/build-model'
28-
import buildJsonSpec, { JsonSpec } from './model/json-spec'
24+
import { compileSpecification } from './model/build-model'
2925
import { ValidationErrors } from './validation-errors'
3026

3127
type StepFunction = (model: Model, errors: ValidationErrors) => Promise<Model>
@@ -41,7 +37,6 @@ type StepFunction = (model: Model, errors: ValidationErrors) => Promise<Model>
4137
export default class Compiler {
4238
queue: StepFunction[]
4339
model: Model
44-
jsonSpec: Map<string, JsonSpec>
4540
errors: ValidationErrors
4641
specsFolder: string
4742
outputFolder: string
@@ -54,9 +49,7 @@ export default class Compiler {
5449
}
5550

5651
generateModel (): this {
57-
this.jsonSpec = buildJsonSpec()
58-
const endpoints = compileEndpoints(this.jsonSpec)
59-
this.model = compileSpecification(endpoints, this.specsFolder, this.outputFolder)
52+
this.model = compileSpecification(this.specsFolder, this.outputFolder)
6053
return this
6154
}
6255

compiler/src/model/build-model.ts

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
TypeAliasDeclaration
3333
} from 'ts-morph'
3434
import * as model from './metamodel'
35-
import { JsonSpec } from './json-spec'
3635
import {
3736
assert,
3837
customTypes,
@@ -57,28 +56,10 @@ import {
5756
mediaTypeToStringArray
5857
} from './utils'
5958

60-
export function compileEndpoints (jsonSpec: Map<string, JsonSpec>): Record<string, model.Endpoint> {
61-
// Create endpoints and merge them with
62-
// the recorded mappings if present.
63-
const map = {}
64-
for (const [api, spec] of jsonSpec.entries()) {
65-
map[api] = {
66-
name: api,
67-
description: null,
68-
docUrl: null,
69-
request: null,
70-
requestBodyRequired: false,
71-
response: null,
72-
urls: []
73-
}
74-
map[api].availability = {}
75-
}
76-
return map
77-
}
78-
79-
export function compileSpecification (endpointMappings: Record<string, model.Endpoint>, specsFolder: string, outputFolder: string): model.Model {
59+
export function compileSpecification (specsFolder: string, outputFolder: string): model.Model {
8060
const tsConfigFilePath = join(specsFolder, 'tsconfig.json')
8161
const project = new Project({ tsConfigFilePath })
62+
const endpointMappings: Record<string, model.Endpoint> = {}
8263

8364
verifyUniqueness(project)
8465

@@ -117,9 +98,6 @@ export function compileSpecification (endpointMappings: Record<string, model.End
11798
definedButNeverUsed.join('\n'),
11899
{ encoding: 'utf8', flag: 'w' }
119100
)
120-
for (const endpoint of Object.values(endpointMappings)) {
121-
model.endpoints.push(endpoint)
122-
}
123101

124102
// Visit all class, interface, enum and type alias definitions
125103
for (const declaration of declarations.classes) {
@@ -141,6 +119,11 @@ export function compileSpecification (endpointMappings: Record<string, model.End
141119
// Sort the types in alphabetical order
142120
sortTypeDefinitions(model.types)
143121

122+
const sortedEndpointKeys = Object.keys(endpointMappings).sort()
123+
for (const key of sortedEndpointKeys) {
124+
model.endpoints.push(endpointMappings[key])
125+
}
126+
144127
return model
145128
}
146129

@@ -191,8 +174,8 @@ function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | Int
191174
throw new Error(`Cannot find url template for ${namespace}, very likely the specification folder does not follow the rest-api-spec`)
192175
}
193176

194-
if (type.description) {
195-
mapping.description = type.description || ''
177+
if (type.description !== '' && type.description !== null && type.description !== undefined) {
178+
mapping.description = type.description
196179
}
197180

198181
let pathMember: Node | null = null

compiler/src/model/utils.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import {
3535
Node,
3636
Project
3737
} from 'ts-morph'
38-
import { closest } from 'fastest-levenshtein'
3938
import semver from 'semver'
4039
import chalk from 'chalk'
4140
import * as model from './metamodel'
@@ -629,6 +628,23 @@ function setTags<TType extends model.BaseType | model.Property | model.EnumMembe
629628
}
630629
}
631630

631+
export function updateEndpoints (mappings: Record<string, model.Endpoint>, name: string): model.Endpoint {
632+
mappings[name] = {
633+
name: name,
634+
// @ts-expect-error TODO
635+
description: null,
636+
// @ts-expect-error TODO
637+
docUrl: null,
638+
request: null,
639+
requestBodyRequired: false,
640+
response: null,
641+
urls: []
642+
}
643+
mappings[name].availability = {}
644+
645+
return mappings[name]
646+
}
647+
632648
/** Lifts jsDoc type annotations to request properties */
633649
export function hoistRequestAnnotations (
634650
request: model.Request, jsDocs: JSDoc[], mappings: Record<string, model.Endpoint>, response: model.TypeName | null
@@ -651,9 +667,7 @@ export function hoistRequestAnnotations (
651667
assert(jsDocs, apiName !== '' && apiName !== null && apiName !== undefined,
652668
`Request ${request.name.name} does not declare the @rest_spec_name to link back to`)
653669

654-
const endpoint = mappings[apiName]
655-
assert(jsDocs, endpoint != null, `The api '${apiName}' does not exists, did you mean '${closest(apiName, Object.keys(mappings))}'?`)
656-
670+
const endpoint = updateEndpoints(mappings, apiName)
657671
endpoint.request = request.name
658672
endpoint.response = response
659673

0 commit comments

Comments
 (0)