-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(ts): strict mode #696
Changes from all commits
b1812d7
2da058c
ad29dbd
afb2ba3
511991d
bea445b
ea5516e
05fcea3
e699eda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import type { InstallerOptions } from '../language'; | |
import type Oas from 'oas'; | ||
import type { Operation } from 'oas'; | ||
import type { HttpMethods, SchemaObject } from 'oas/dist/rmoas.types'; | ||
import type { SemVer } from 'semver'; | ||
import type { | ||
ClassDeclaration, | ||
JSDocStructure, | ||
|
@@ -53,9 +54,9 @@ export default class TSGenerator extends CodeGeneratorLanguage { | |
|
||
types: Map<string, string>; | ||
|
||
files: Record<string, string>; | ||
files?: Record<string, string>; | ||
|
||
sdk: ClassDeclaration; | ||
sdk!: ClassDeclaration; | ||
|
||
schemas: Record< | ||
string, | ||
|
@@ -138,7 +139,7 @@ export default class TSGenerator extends CodeGeneratorLanguage { | |
if (!pkgVersion) { | ||
// If the version that's in `info.version` isn't compatible with semver NPM won't be able to | ||
// handle it properly so we need to fallback to something it can. | ||
pkgVersion = semver.coerce('0.0.0'); | ||
pkgVersion = semver.coerce('0.0.0') as SemVer; | ||
} | ||
|
||
const pkg: PackageJson = { | ||
|
@@ -221,7 +222,7 @@ export default class TSGenerator extends CodeGeneratorLanguage { | |
sdkSource | ||
.getImportDeclarations() | ||
.find(id => id.getText() === "import type * as types from './types';") | ||
.remove(); | ||
?.remove(); | ||
} | ||
|
||
// If this SDK doesn't use the `HTTPMethodRange` interface for handling `2XX` response status | ||
|
@@ -230,7 +231,7 @@ export default class TSGenerator extends CodeGeneratorLanguage { | |
sdkSource | ||
.getImportDeclarations() | ||
.find(id => id.getText().includes('HTTPMethodRange')) | ||
.replaceWithText("import type { ConfigOptions, FetchResponse } from '@api/core'"); | ||
?.replaceWithText("import type { ConfigOptions, FetchResponse } from '@api/core'"); | ||
} | ||
|
||
if (this.outputJS) { | ||
|
@@ -566,10 +567,10 @@ sdk.server('https://eu.api.example.com/v14');`), | |
|
||
let hasOptionalBody = false; | ||
let hasOptionalMetadata = false; | ||
const parameters: { | ||
body?: OptionalKind<ParameterDeclarationStructure>; | ||
metadata?: OptionalKind<ParameterDeclarationStructure>; | ||
} = {}; | ||
const parameters = {} as { | ||
body: OptionalKind<ParameterDeclarationStructure>; | ||
metadata: OptionalKind<ParameterDeclarationStructure>; | ||
}; | ||
Comment on lines
+570
to
+573
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This slight reshuffling ended up fixing all of those There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotta love strict mode |
||
|
||
if (paramTypes) { | ||
// If an operation has a request body payload it will only ever have `body` or `formData`, | ||
|
@@ -656,7 +657,7 @@ sdk.server('https://eu.api.example.com/v14');`), | |
// we should only add a docblock to the first overload we create because IDE Intellisense will | ||
// always use that and adding a docblock to all three will bloat the SDK with unused and | ||
// unsurfaced method documentation. | ||
docs: shouldAddAltTypedOverloads ? null : Object.keys(docblock).length ? [docblock] : null, | ||
docs: shouldAddAltTypedOverloads ? undefined : Object.keys(docblock).length ? [docblock] : undefined, | ||
statements: writer => { | ||
/** | ||
* @example return this.core.fetch('/pet/findByStatus', 'get', body, metadata); | ||
|
@@ -698,7 +699,7 @@ sdk.server('https://eu.api.example.com/v14');`), | |
{ ...parameters.metadata, hasQuestionToken: false }, | ||
], | ||
returnType, | ||
docs: Object.keys(docblock).length ? [docblock] : null, | ||
docs: Object.keys(docblock).length ? [docblock] : undefined, | ||
}); | ||
|
||
// Create an overload that just has a single `metadata` parameter. | ||
|
@@ -739,13 +740,13 @@ sdk.server('https://eu.api.example.com/v14');`), | |
*/ | ||
loadOperationsAndMethods() { | ||
const operations: Record</* operationId */ string, OperationTypeHousing> = {}; | ||
const methods = new Set(); | ||
const methods = new Set<HttpMethods>(); | ||
|
||
// Prepare all of the schemas that we need to process for every operation within this API | ||
// definition. | ||
Object.entries(this.spec.getPaths()).forEach(([, ops]) => { | ||
Object.entries(ops).forEach(([method, operation]: [HttpMethods, Operation]) => { | ||
methods.add(method); | ||
Object.entries(ops).forEach(([method, operation]: [string, Operation]) => { | ||
methods.add(method as HttpMethods); | ||
|
||
const operationId = operation.getOperationId({ | ||
// This `camelCase` option will clean up any weird characters that might be present in | ||
|
@@ -786,7 +787,7 @@ sdk.server('https://eu.api.example.com/v14');`), | |
transformer: (s: SchemaObject) => { | ||
// As our schemas are dereferenced in the `oas` library we don't want to pollute our | ||
// codegen'd schemas file with duplicate schemas. | ||
if ('x-readme-ref-name' in s) { | ||
if ('x-readme-ref-name' in s && typeof s['x-readme-ref-name'] !== 'undefined') { | ||
const typeName = generateTypeName(s['x-readme-ref-name']); | ||
this.addSchemaToExport(s, typeName, typeName); | ||
|
||
|
@@ -844,7 +845,7 @@ sdk.server('https://eu.api.example.com/v14');`), | |
transformer: (s: SchemaObject) => { | ||
// As our schemas are dereferenced in the `oas` library we don't want to pollute our | ||
// codegen'd schemas file with duplicate schemas. | ||
if ('x-readme-ref-name' in s) { | ||
if ('x-readme-ref-name' in s && typeof s['x-readme-ref-name'] !== 'undefined') { | ||
const typeName = generateTypeName(s['x-readme-ref-name']); | ||
this.addSchemaToExport(s, typeName, `${typeName}`); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,9 @@ | |
"esModuleInterop": true, | ||
"lib": ["DOM", "DOM.Iterable", "ES2020"], | ||
"noImplicitAny": true, | ||
"outDir": "dist/" | ||
"outDir": "dist/", | ||
"strict": true, | ||
"useUnknownInCatchVariables": false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Allows us to not have to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh hell yeah I've been running into this a bunch in |
||
}, | ||
"include": ["./src/**/*"] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You originally had these as the following but I think optionally chaining these is a little nicer.
Cool with the original solution if you want me to revert it though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I suppose this just returns
undefined
and doesn't actually touch the originalsdkSource
variable if it can't find anything right? Works for me!