Skip to content

Commit

Permalink
Prepare for ts 5.5 by adding return definitions (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericanderson authored May 29, 2024
1 parent 0ecd42b commit 7e8d065
Show file tree
Hide file tree
Showing 83 changed files with 260 additions and 157 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"mytsup",
"pino",
"todoapp"
]
],
"typescript.tsdk": "node_modules/typescript/lib"
}
3 changes: 2 additions & 1 deletion examples-extra/basic/cli/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import type { Client } from "@osdk/client/unstable-do-not-use";
import { createClient } from "@osdk/client/unstable-do-not-use";
import invariant from "tiny-invariant";
import { logger } from "./logger.js";
Expand All @@ -22,7 +23,7 @@ import { loggingFetch } from "./loggingFetch.js";
invariant(process.env.FOUNDRY_STACK !== undefined);
invariant(process.env.FOUNDRY_USER_TOKEN !== undefined);

export const client = createClient(
export const client: Client = createClient(
process.env.FOUNDRY_STACK,
"ri.ontology.main.ontology.00000000-0000-0000-0000-000000000000",
async () => process.env.FOUNDRY_USER_TOKEN!,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli.common/src/ExitProcessError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class ExitProcessError extends Error {
public readonly errorCode: number,
public readonly msg?: string,
public readonly tip?: string,
public readonly originalError?: Error,
public readonly originalError?: Error | undefined,
) {
super(msg);
}
Expand Down
6 changes: 4 additions & 2 deletions packages/cli.common/src/commands/auth/login/loginFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import { exit } from "node:process";
import { parse } from "node:url";
import open from "open";
import type { LoginArgs } from "./LoginArgs.js";
import type { TokenResponse } from "./token.js";
import type { TokenResponse, TokenSuccessResponse } from "./token.js";
import { isTokenErrorResponse } from "./token.js";

const BROWSER_PROMPT_TIME_MS = 60 * 1000;

export async function invokeLoginFlow(args: LoginArgs) {
export async function invokeLoginFlow(
args: LoginArgs,
): Promise<TokenSuccessResponse> {
consola.start(`Authenticating using application id: ${args.clientId}`);
const redirectUrl = "http://localhost:8080/auth/callback";
const port = parse(redirectUrl).port;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli.common/src/yargs/logLevelMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { consola } from "consola";
import type { CliCommonArgs } from "../CliCommonArgs.js";

let firstTime = true;
export async function logLevelMiddleware(args: CliCommonArgs) {
export async function logLevelMiddleware(args: CliCommonArgs): Promise<void> {
if (firstTime) {
firstTime = false;

Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface CliArgs {
corsProxy?: boolean;
}

export async function cli(args: string[] = process.argv) {
export async function cli(args: string[] = process.argv): Promise<void> {
const base: Argv<CliArgs> = yargs(hideBin(args))
.version(process.env.PACKAGE_VERSION ?? "")
.wrap(Math.min(150, yargs().terminalWidth()))
Expand Down
3 changes: 2 additions & 1 deletion packages/create-app/src/consola.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* limitations under the License.
*/

import type { ConsolaInstance } from "consola";
import { consola as defaultConsola, createConsola } from "consola";

export const consola = createConsola({
export const consola: ConsolaInstance = createConsola({
// https://github.com/unjs/consola/issues/251
async prompt(...params) {
const response = (await defaultConsola.prompt(...params)) as any;
Expand Down
4 changes: 2 additions & 2 deletions packages/create-app/src/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

import { colorize } from "consola/utils";

export function green(text: string) {
export function green(text: string): string {
return colorize("green", text);
}

export function italic(text: string) {
export function italic(text: string): string {
return colorize("italic", text);
}
2 changes: 1 addition & 1 deletion packages/create-app/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function run(
osdkRegistryUrl,
corsProxy,
}: RunArgs,
) {
): Promise<void> {
consola.log("");
consola.start(
`Creating project ${green(project)} using template ${green(template.id)}`,
Expand Down
2 changes: 1 addition & 1 deletion packages/example-generator/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface CliArgs {
check: boolean;
}

export async function cli(args: string[] = process.argv) {
export async function cli(args: string[] = process.argv): Promise<void> {
const base: Argv<CliArgs> = yargs(hideBin(args))
.version(false)
.strict()
Expand Down
2 changes: 1 addition & 1 deletion packages/example-generator/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface RunArgs {
check: boolean;
}

export async function run({ outputDirectory, check }: RunArgs) {
export async function run({ outputDirectory, check }: RunArgs): Promise<void> {
const resolvedOutput = path.resolve(outputDirectory);
const tmpDir = createTmpDir();
await generateExamples(tmpDir);
Expand Down
4 changes: 3 additions & 1 deletion packages/gateway-generator/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { GenerateCommand } from "./generateCommand.js";

export async function cli(args: string[] = process.argv) {
export async function cli(
args: string[] = process.argv,
): Promise<{ [x: string]: unknown; _: (string | number)[]; $0: string }> {
const base = yargs(hideBin(args))
.version(false)
.command(new GenerateCommand())
Expand Down
6 changes: 3 additions & 3 deletions packages/gateway-generator/src/cli/generateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ export interface GenerateOpenApiArgs {
}

export class GenerateCommand implements CommandModule<{}, GenerateOpenApiArgs> {
public aliases = [];

public command = "generate";

public describe = "Generate TypeScript bindings for a OpenApi API";
Expand Down Expand Up @@ -62,7 +60,9 @@ export class GenerateCommand implements CommandModule<{}, GenerateOpenApiArgs> {
});
}

public handler = async (args: Arguments<GenerateOpenApiArgs>) => {
public handler = async (
args: Arguments<GenerateOpenApiArgs>,
): Promise<void> => {
const generateVisitors = args.generateVisitors ?? true;
const input = args.inputFile;
const output = args.outputDir;
Expand Down
5 changes: 4 additions & 1 deletion packages/gateway-generator/src/cli/updateSls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export interface Manifest {
};
}

export async function updateSls(manifest: Manifest, output: string) {
export async function updateSls(
manifest: Manifest,
output: string,
): Promise<void> {
// the whole manifest shape is hand-jammed so lets have a quick sanity check to make sure we're ok
if (manifest["manifest-version"] !== "1.0") {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion packages/gateway-generator/src/generate/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function generateRequestType(
outputDir: string,
project: Project,
addCopyright: (sf: SourceFile) => void,
) {
): void {
const directory = project.createDirectory(`${outputDir}/request`);
const sourceFile = directory.createSourceFile(`OpenApiRequest.ts`);

Expand Down
2 changes: 1 addition & 1 deletion packages/gateway-generator/src/generate/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function visitTypeUnion<T extends { type: string }, R>(
return visitor.unknown(value);
}

export function generateDocumentation(documentation: Documentation) {
export function generateDocumentation(documentation: Documentation): string {
if (Object.keys(documentation).length === 0) {
return "";
}
Expand Down
2 changes: 1 addition & 1 deletion packages/gateway-generator/src/generate/component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Project } from "ts-morph";
import { describe, expect, it } from "vitest";
import { generateComponent } from "./component.js";

export function addCopyrightForTest(sf: SourceFile) {
export function addCopyrightForTest(sf: SourceFile): void {
sf.insertText(
0,
`/*
Expand Down
4 changes: 2 additions & 2 deletions packages/gateway-generator/src/generate/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function generateComponents(
project: Project,
options: GenerateOptions,
addCopyright: (sf: SourceFile) => void,
) {
): void {
const directory = project.createDirectory(`${outputDir}/components`);
components.forEach(component =>
generateComponent(component, directory, options, addCopyright)
Expand All @@ -39,7 +39,7 @@ export function generateComponent(
directory: Directory,
options: GenerateOptions,
addCopyright: (sf: SourceFile) => void,
) {
): void {
const sourceFile = directory.createSourceFile(`${component.name}.ts`);
const referenceSet = new Set<string>();

Expand Down
4 changes: 2 additions & 2 deletions packages/gateway-generator/src/generate/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function generateErrors(
outputDir: string,
project: Project,
addCopyright: (sf: SourceFile) => void,
) {
): void {
const directory = project.createDirectory(`${outputDir}/errors`);
errors.forEach(error => generateError(error, directory, addCopyright));
}
Expand All @@ -35,7 +35,7 @@ export function generateError(
error: Error,
directory: Directory,
addCopyright: (sf: SourceFile) => void,
) {
): void {
const sourceFile = directory.createSourceFile(`${error.name}.ts`);

const referenceSet = new Set<string>();
Expand Down
2 changes: 1 addition & 1 deletion packages/gateway-generator/src/generate/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function generate(
apiSpec: ApiSpec,
outputDirectory: string,
options: GenerateOptions,
) {
): Promise<void> {
const project = new Project({
compilerOptions: {
declaration: true,
Expand Down
6 changes: 3 additions & 3 deletions packages/gateway-generator/src/generate/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function generateNamespaces(
outputDir: string,
project: Project,
addCopyright: (sf: SourceFile) => void,
) {
): void {
const directory = project.createDirectory(`${outputDir}/namespaces`);
namespaces.forEach(namespace =>
generateNamespace(namespace, directory, addCopyright)
Expand All @@ -66,7 +66,7 @@ export function generateNamespace(
namespace: Namespace,
directory: Directory,
addCopyright: (sf: SourceFile) => void,
) {
): void {
const sourceFile = directory.createSourceFile(`${namespace.name}.ts`);

const referenceSet = new Set<string>();
Expand Down Expand Up @@ -133,7 +133,7 @@ export function generateResource(
resource: Resource,
sourceFile: SourceFile,
referenceSet: Set<string>,
) {
): SourceFile {
const getMethods = resource.staticOperations.map(staticOperation =>
generateOperationAsFunction(staticOperation, referenceSet)
);
Expand Down
4 changes: 2 additions & 2 deletions packages/gateway-generator/src/generate/reserveredKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ const reservedKeywords = new Set([
"yield",
]);

export function addUnderscoreIfIsReservedKeyword(name: string) {
export function addUnderscoreIfIsReservedKeyword(name: string): string {
if (isReservedKeyword(name)) {
return `${name}_`;
}
return name;
}

export function isReservedKeyword(name: string) {
export function isReservedKeyword(name: string): boolean {
return reservedKeywords.has(name);
}
10 changes: 9 additions & 1 deletion packages/maker/src/api/defineInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ export function defineInterface(
SharedPropertyType | PropertyTypeType
>;
},
) {
): {
apiName: string;
displayName: string;
extendsInterfaces: never[];
links: {};
properties: { [k: string]: SharedPropertyType };
rid: string;
description: string;
} {
invariant(
ontologyDefinition.interfaceTypes[apiName] === undefined,
`Interface ${apiName} already exists`,
Expand Down
19 changes: 18 additions & 1 deletion packages/maker/src/api/defineOntology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,24 @@ export let namespace: string;
export async function defineOntology(
ns: string,
body: () => void | Promise<void>,
) {
): Promise<
{
sharedPropertyTypes: { [k: string]: Gateway.SharedPropertyType };
interfaceTypes: { [k: string]: Gateway.InterfaceType };
objectTypes: Record<
Gateway.ObjectTypeApiName,
Gateway.ObjectTypeFullMetadata
>;
actionTypes: Record<Gateway.ActionTypeApiName, Gateway.ActionTypeV2>;
queryTypes: Record<Gateway.QueryApiName, Gateway.QueryTypeV2>;
ontology: {
apiName: string;
description: string;
displayName: string;
rid: string;
};
}
> {
namespace = ns;
ontologyDefinition = {
actionTypes: {},
Expand Down
4 changes: 3 additions & 1 deletion packages/maker/src/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import { defineObject } from "../api/defineObject.js";
import { defineOntology } from "../api/defineOntology.js";
import { defineSharedPropertyType } from "../api/defineSpt.js";

export default async function main(args: string[] = process.argv) {
export default async function main(
args: string[] = process.argv,
): Promise<void> {
const commandLineOpts: {
input: string;
output: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function addPackagesToPackageJson(
packageJsonPath: string,
packagesToAdd: string[],
section: "dependencies" | "devDependencies" = "dependencies",
) {
): Promise<void> {
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, "utf-8"));
Object.assign(
packageJson[section],
Expand Down
8 changes: 5 additions & 3 deletions packages/platform-sdk-generator/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import { generatePlatformSdkV2 } from "./generatePlatformSdkv2.js";
import type { ApiSpec } from "./ir/index.js";
import { updateSls } from "./updateSls.js";

export async function cli(args: string[] = process.argv) {
export async function cli(
args: string[] = process.argv,
): Promise<{ [x: string]: unknown; _: (string | number)[]; $0: string }> {
const base = yargs(hideBin(args))
.version(false)
.command(new GenerateCommand())
Expand All @@ -42,7 +44,7 @@ export interface Options {
}

export class GenerateCommand implements CommandModule<{}, Options> {
public aliases = [];
public aliases = [] as const;

public command = "generate";

Expand Down Expand Up @@ -72,7 +74,7 @@ export class GenerateCommand implements CommandModule<{}, Options> {
});
}

public handler = async (args: Arguments<Options>) => {
public handler = async (args: Arguments<Options>): Promise<void> => {
const input = args.inputFile;
const output = args.outputDir;

Expand Down
4 changes: 2 additions & 2 deletions packages/platform-sdk-generator/src/generateImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import type { Component } from "./model/Component.js";
import type { Namespace } from "./model/Namespace.js";
import { groupByAsMap } from "./util/groupByAsMap.js";

export const SKIP = Symbol("SKIP");
export const SKIP: unique symbol = Symbol("SKIP");

export function generateImports(
referencedComponents: Set<Component>,
namespaceMapping: Map<Namespace, string | typeof SKIP>,
) {
): string {
const groups = groupByAsMap(referencedComponents, "namespace");
const imports = [...groups.entries()].filter(([ns]) =>
namespaceMapping.get(ns) !== SKIP
Expand Down
Loading

0 comments on commit 7e8d065

Please sign in to comment.