Skip to content

Commit c00e06d

Browse files
authored
Rename request and response types (elastic#375)
1 parent 0009802 commit c00e06d

File tree

715 files changed

+80577
-80867
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

715 files changed

+80577
-80867
lines changed

compiler/model/build-model.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,14 @@ export function compileSpecification (endpointMappings: Record<string, model.End
146146

147147
// Sort the types in alphabetical order
148148
model.types.sort((a, b) => {
149-
if (a.name.name > b.name.name) return 1
150-
if (a.name.name < b.name.name) return -1
149+
if (a.name.namespace === b.name.namespace) {
150+
if (a.name.name > b.name.name) return 1
151+
if (a.name.name < b.name.name) return -1
152+
return 0
153+
}
154+
155+
if (a.name.namespace > b.name.namespace) return 1
156+
if (a.name.namespace < b.name.namespace) return -1
151157
return 0
152158
})
153159

@@ -177,13 +183,10 @@ function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | Int
177183
// Request definitions needs to be handled
178184
// differently from normal classes
179185
if (Node.isInterfaceDeclaration(declaration) && isApi(declaration)) {
180-
// It's not guaranteed that every *Request definition
181-
// has an associated *Response definition as well.
182-
const response = allClasses
183-
.map(d => d.getName())
184-
.find(n => n === `${name.slice(0, -7)}Response`) !== undefined
185-
? { name: `${name.slice(0, -7)}Response`, namespace: getNameSpace(declaration) }
186-
: null
186+
const response: model.TypeName = {
187+
name: 'Response',
188+
namespace: getNameSpace(declaration)
189+
}
187190

188191
const type: model.Request = {
189192
kind: 'request',

compiler/steps/validate-rest-spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const LOG = 'ALL' // default: ALL
4040
export default async function validateRestSpec (model: model.Model, jsonSpec: Map<string, JsonSpec>, errors: ValidationErrors): Promise<model.Model> {
4141
for (const endpoint of model.endpoints) {
4242
if (endpoint.request == null) continue
43-
const requestDefinition = getDefinition(endpoint.request.name)
43+
const requestDefinition = getDefinition(endpoint.request)
4444
const requestProperties = getProperties(requestDefinition)
4545
if (endpoint.request.name === LOG || LOG === 'ALL') {
4646
const spec = jsonSpec.get(endpoint.name)
@@ -98,15 +98,15 @@ export default async function validateRestSpec (model: model.Model, jsonSpec: Ma
9898

9999
return model
100100

101-
function getDefinition (name: string): model.Request | model.Interface {
101+
function getDefinition (name: model.TypeName): model.Request | model.Interface {
102102
for (const type of model.types) {
103103
if (type.kind === 'request' || type.kind === 'interface') {
104-
if (type.name.name === name) {
104+
if (type.name.name === name.name && type.name.namespace === name.namespace) {
105105
return type
106106
}
107107
}
108108
}
109-
throw new Error(`Can't find the request definiton for ${name}`)
109+
throw new Error(`Can't find the request definiton for ${name.namespace}.${name.name}`)
110110
}
111111

112112
// recursively gets the properties from the current and inherited classes
@@ -134,7 +134,7 @@ export default async function validateRestSpec (model: model.Model, jsonSpec: Ma
134134
}
135135

136136
if (Array.isArray(definition.inherits)) {
137-
const inherits = definition.inherits.map(inherit => getDefinition(inherit.type.name))
137+
const inherits = definition.inherits.map(inherit => getDefinition(inherit.type))
138138
for (const inherit of inherits) {
139139
const properties = getProperties(inherit)
140140
if (properties.path.length > 0) {

compiler/update-imports.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,26 @@ Example: npm run imports:fix -- --help
4949
const specsFolder = join(__dirname, '..', 'specification')
5050
const tsConfigFilePath = join(specsFolder, 'tsconfig.json')
5151

52+
const aliasedImports: string[] = []
53+
5254
async function removeImports (): Promise<void> {
5355
if (options.rebuild !== true) return
5456
const project = new Project({ tsConfigFilePath })
5557
for (const sourceFile of project.getSourceFiles()) {
5658
if (typeof options.file === 'string' && !sourceFile.getFilePath().includes(options.file)) continue
5759
console.log(`Removing imports in ${sourceFile.getFilePath().replace(/.*[/\\]specification[/\\]?/, '')}`)
5860
for (const declaration of sourceFile.getImportDeclarations()) {
59-
declaration.remove()
61+
let hasImportAlias = false
62+
for (const namedImport of declaration.getNamedImports()) {
63+
if (namedImport.getAliasNode() != null) {
64+
aliasedImports.push(sourceFile.getFilePath().replace(/.*[/\\]specification[/\\]?/, ''))
65+
hasImportAlias = true
66+
break
67+
}
68+
}
69+
if (!hasImportAlias) {
70+
declaration.remove()
71+
}
6072
}
6173
}
6274
await project.save()
@@ -84,7 +96,11 @@ async function fixImports (): Promise<void> {
8496
removeImports()
8597
.then(fixImports)
8698
.then(() => {
87-
console.log('Done!')
99+
if (aliasedImports.length > 0) {
100+
console.log('\nThere are some import with aliases that can\'t be rebuilt automatically, run `make spec-compile` to verify if those need to be fixed manually:')
101+
console.log(aliasedImports.join('\n'))
102+
}
103+
console.log('\nDone!')
88104
})
89105
.catch(err => {
90106
console.log(err)

docs/specification-structure.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,37 @@ For example: [`/specification/_global/search`](../specification/_global/search).
4242
### Specification helpers
4343

4444
The specification has a set of custom types used to define complex structures
45-
or behaviors. Those types must be placed in [`/specification/_spec_utils`](../specification/_spec_utils).
45+
or behaviors. Those types must be placed in [`/specification/_spec_utils`](../specification/_spec_utils).
46+
47+
### Import alias
48+
49+
It can happen that you need to import a type from a different namespace that already exist in the current file.
50+
To solve this problem, you should use import aliases.
51+
52+
```ts
53+
// _global/get/GetRequest.ts
54+
55+
import { RequestBase } from '@_types/Base'
56+
57+
/**
58+
* @rest_spec_name get
59+
* @since 0.0.0
60+
* @stability TODO
61+
*/
62+
export interface Request extends RequestBase {
63+
path_parts: {}
64+
query_parameters?: {}
65+
}
66+
```
67+
```ts
68+
// _global/get_source/SourceRequest.ts
69+
70+
import { Request as GetRequest } from '_global/get/GetRequest'
71+
72+
/**
73+
* @rest_spec_name get_source
74+
* @since 0.0.0
75+
* @stability TODO
76+
*/
77+
export interface Request extends GetRequest {}
78+
```

0 commit comments

Comments
 (0)