Skip to content

Commit

Permalink
private/apigen: Fix TS generator to align with linter
Browse files Browse the repository at this point in the history
I introduced some subtle linter issues when I added the APIError class
and added the TypeScript mock generator.

This commit addresses them, so the linter doesn't yell about the
TypeScript generated sources.

Change-Id: Icc7dfa4169a228b1a5144d4a292f4350ee5ef9f0
  • Loading branch information
ifraixedes committed Nov 3, 2023
1 parent f6e357b commit 48877c0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 40 deletions.
32 changes: 16 additions & 16 deletions private/apigen/example/client-api-mock.gen.ts
Expand Up @@ -39,7 +39,7 @@ export class Metadata {
tags?: string[][];
}

export class UsersCreateRequestItem {
export class UsersGetResponseItem {
name: string;
surname: string;
email: string;
Expand All @@ -54,9 +54,9 @@ export type DocsGetResponse = Array<DocsGetResponseItem>

export type DocsGetResponseItemLastRetrievals = Array<DocsGetResponseItemLastRetrievalsItem>

export type UsersCreateRequest = Array<UsersCreateRequestItem>
export type UsersCreateRequest = Array<UsersGetResponseItem>

export type UsersGetResponse = Array<UsersCreateRequestItem>
export type UsersGetResponse = Array<UsersGetResponseItem>

class APIError extends Error {
constructor(
Expand Down Expand Up @@ -87,43 +87,43 @@ export class DocumentsHttpApiV0 {
}

public async get(): Promise<DocsGetResponse> {
if (this.respStatusCode != 0) {
if (this.respStatusCode !== 0) {
throw new APIError('mock error message: ' + this.respStatusCode, this.respStatusCode);
}

return JSON.parse("[{\"id\":\"00000000-0000-0000-0000-000000000000\",\"path\":\"/workspace/notes.md\",\"date\":\"0001-01-01T00:00:00Z\",\"metadata\":{\"owner\":\"Storj\",\"tags\":[[\"category\",\"general\"]]},\"last_retrievals\":[{\"user\":\"Storj\",\"when\":\"2023-10-19T12:54:40.418932461+02:00\"}]}]") as DocsGetResponse;
return JSON.parse('[{"id":"00000000-0000-0000-0000-000000000000","path":"/workspace/notes.md","date":"0001-01-01T00:00:00Z","metadata":{"owner":"Storj","tags":[["category","general"]]},"last_retrievals":[{"user":"Storj","when":"2023-11-03T15:41:14.288101181+01:00"}]}]') as DocsGetResponse;
}

public async getOne(path: string): Promise<Document> {
if (this.respStatusCode != 0) {
if (this.respStatusCode !== 0) {
throw new APIError('mock error message: ' + this.respStatusCode, this.respStatusCode);
}

return JSON.parse("{\"id\":\"00000000-0000-0000-0000-000000000000\",\"date\":\"2023-10-18T13:54:40.418935224+02:00\",\"pathParam\":\"ID\",\"body\":\"## Notes\",\"version\":{\"date\":\"2023-10-19T13:24:40.418935292+02:00\",\"number\":1}}") as Document;
return JSON.parse('{"id":"00000000-0000-0000-0000-000000000000","date":"2023-11-02T16:41:14.288104016+01:00","pathParam":"ID","body":"## Notes","version":{"date":"2023-11-03T16:11:14.288104112+01:00","number":1}}') as Document;
}

public async getTag(path: string, tagName: string): Promise<string[]> {
if (this.respStatusCode != 0) {
if (this.respStatusCode !== 0) {
throw new APIError('mock error message: ' + this.respStatusCode, this.respStatusCode);
}

return JSON.parse("[\"category\",\"notes\"]") as string[];
return JSON.parse('["category","notes"]') as string[];
}

public async getVersions(path: string): Promise<Version[]> {
if (this.respStatusCode != 0) {
if (this.respStatusCode !== 0) {
throw new APIError('mock error message: ' + this.respStatusCode, this.respStatusCode);
}

return JSON.parse("[{\"date\":\"2023-10-04T13:54:40.418937913+02:00\",\"number\":1},{\"date\":\"2023-10-19T08:54:40.418937979+02:00\",\"number\":2}]") as Version[];
return JSON.parse('[{"date":"2023-10-19T17:41:14.288106868+02:00","number":1},{"date":"2023-11-03T11:41:14.288106932+01:00","number":2}]') as Version[];
}

public async updateContent(request: DocsUpdateContentRequest, path: string, id: UUID, date: Time): Promise<DocsUpdateContentResponse> {
if (this.respStatusCode != 0) {
if (this.respStatusCode !== 0) {
throw new APIError('mock error message: ' + this.respStatusCode, this.respStatusCode);
}

return JSON.parse("{\"id\":\"00000000-0000-0000-0000-000000000000\",\"date\":\"2023-10-19T13:54:40.418939503+02:00\",\"pathParam\":\"ID\",\"body\":\"## Notes\\n### General\"}") as DocsUpdateContentResponse;
return JSON.parse('{"id":"00000000-0000-0000-0000-000000000000","date":"2023-11-03T16:41:14.288108437+01:00","pathParam":"ID","body":"## Notes\n### General"}') as DocsUpdateContentResponse;
}
}

Expand All @@ -147,15 +147,15 @@ export class UsersHttpApiV0 {
}

public async get(): Promise<UsersGetResponse> {
if (this.respStatusCode != 0) {
if (this.respStatusCode !== 0) {
throw new APIError('mock error message: ' + this.respStatusCode, this.respStatusCode);
}

return JSON.parse("[{\"name\":\"Storj\",\"surname\":\"Labs\",\"email\":\"storj@storj.test\"},{\"name\":\"Test1\",\"surname\":\"Testing\",\"email\":\"test1@example.test\"},{\"name\":\"Test2\",\"surname\":\"Testing\",\"email\":\"test2@example.test\"}]") as UsersGetResponse;
return JSON.parse('[{"name":"Storj","surname":"Labs","email":"storj@storj.test"},{"name":"Test1","surname":"Testing","email":"test1@example.test"},{"name":"Test2","surname":"Testing","email":"test2@example.test"}]') as UsersGetResponse;
}

public async create(request: UsersCreateRequest): Promise<void> {
if (this.respStatusCode != 0) {
if (this.respStatusCode !== 0) {
throw new APIError('mock error message: ' + this.respStatusCode, this.respStatusCode);
}

Expand Down
20 changes: 10 additions & 10 deletions private/apigen/example/client-api.gen.ts
Expand Up @@ -41,7 +41,7 @@ export class Metadata {
tags?: string[][];
}

export class UsersGetResponseItem {
export class UsersCreateRequestItem {
name: string;
surname: string;
email: string;
Expand All @@ -56,9 +56,9 @@ export type DocsGetResponse = Array<DocsGetResponseItem>

export type DocsGetResponseItemLastRetrievals = Array<DocsGetResponseItemLastRetrievalsItem>

export type UsersCreateRequest = Array<UsersGetResponseItem>
export type UsersCreateRequest = Array<UsersCreateRequestItem>

export type UsersGetResponse = Array<UsersGetResponseItem>
export type UsersGetResponse = Array<UsersCreateRequestItem>

class APIError extends Error {
constructor(
Expand All @@ -80,7 +80,7 @@ export class DocumentsHttpApiV0 {
return response.json().then((body) => body as DocsGetResponse);
}
const err = await response.json();
throw new APIError(err.error,response.status);
throw new APIError(err.error, response.status);
}

public async getOne(path: string): Promise<Document> {
Expand All @@ -90,7 +90,7 @@ export class DocumentsHttpApiV0 {
return response.json().then((body) => body as Document);
}
const err = await response.json();
throw new APIError(err.error,response.status);
throw new APIError(err.error, response.status);
}

public async getTag(path: string, tagName: string): Promise<string[]> {
Expand All @@ -100,7 +100,7 @@ export class DocumentsHttpApiV0 {
return response.json().then((body) => body as string[]);
}
const err = await response.json();
throw new APIError(err.error,response.status);
throw new APIError(err.error, response.status);
}

public async getVersions(path: string): Promise<Version[]> {
Expand All @@ -110,7 +110,7 @@ export class DocumentsHttpApiV0 {
return response.json().then((body) => body as Version[]);
}
const err = await response.json();
throw new APIError(err.error,response.status);
throw new APIError(err.error, response.status);
}

public async updateContent(request: DocsUpdateContentRequest, path: string, id: UUID, date: Time): Promise<DocsUpdateContentResponse> {
Expand All @@ -123,7 +123,7 @@ export class DocumentsHttpApiV0 {
return response.json().then((body) => body as DocsUpdateContentResponse);
}
const err = await response.json();
throw new APIError(err.error,response.status);
throw new APIError(err.error, response.status);
}
}

Expand All @@ -138,7 +138,7 @@ export class UsersHttpApiV0 {
return response.json().then((body) => body as UsersGetResponse);
}
const err = await response.json();
throw new APIError(err.error,response.status);
throw new APIError(err.error, response.status);
}

public async create(request: UsersCreateRequest): Promise<void> {
Expand All @@ -148,6 +148,6 @@ export class UsersHttpApiV0 {
return;
}
const err = await response.json();
throw new Error(err.error);
throw new APIError(err.error, response.status);
}
}
2 changes: 1 addition & 1 deletion private/apigen/tsgen.go
Expand Up @@ -133,7 +133,7 @@ func (f *tsGenFile) createAPIClient(group *EndpointGroup) {
f.pf("\t\t\t%s", returnStmt)
f.pf("\t\t}")
f.pf("\t\tconst err = await response.json();")
f.pf("\t\tthrow new APIError(err.error,response.status);")
f.pf("\t\tthrow new APIError(err.error, response.status);")
f.pf("\t}")
}
f.pf("}")
Expand Down
4 changes: 2 additions & 2 deletions private/apigen/tsgenmock.go
Expand Up @@ -103,7 +103,7 @@ func (f *tsGenMockFile) createAPIClient(group *EndpointGroup) {
}

f.pf("\tpublic async %s(%s): Promise<%s> {", method.TypeScriptName, funcArgs, returnType)
f.pf("\t\tif (this.respStatusCode != 0) {")
f.pf("\t\tif (this.respStatusCode !== 0) {")
f.pf("\t\t\tthrow new APIError('mock error message: ' + this.respStatusCode, this.respStatusCode);")
f.pf("\t\t}")
f.pf("")
Expand All @@ -118,7 +118,7 @@ func (f *tsGenMockFile) createAPIClient(group *EndpointGroup) {
))
}

f.pf("\t\treturn JSON.parse(%q) as %s;", string(res), returnType)
f.pf("\t\treturn JSON.parse('%s') as %s;", string(res), returnType)
} else {
f.pf("\t\treturn;")
}
Expand Down
7 changes: 6 additions & 1 deletion private/apigen/tstypes.go
Expand Up @@ -144,7 +144,12 @@ func (types *Types) GenerateTypescriptDefinitions() string {
var out StringBuilder
pf := out.Writelnf

pf(types.getTypescriptImports())
{
i := types.getTypescriptImports()
if i != "" {
pf(i)
}
}

allTypes := types.All()
namedTypes := mapToSlice(allTypes)
Expand Down
20 changes: 10 additions & 10 deletions web/satellite/src/api/v0.gen.ts
Expand Up @@ -114,7 +114,7 @@ export class ProjectManagementHttpApiV0 {
return response.json().then((body) => body as Project);
}
const err = await response.json();
throw new APIError(response.status, err.error);
throw new APIError(err.error, response.status);
}

public async updateProject(request: UpsertProjectInfo, id: UUID): Promise<Project> {
Expand All @@ -124,7 +124,7 @@ export class ProjectManagementHttpApiV0 {
return response.json().then((body) => body as Project);
}
const err = await response.json();
throw new APIError(response.status, err.error);
throw new APIError(err.error, response.status);
}

public async deleteProject(id: UUID): Promise<void> {
Expand All @@ -134,7 +134,7 @@ export class ProjectManagementHttpApiV0 {
return;
}
const err = await response.json();
throw new APIError(response.status, err.error);
throw new APIError(err.error, response.status);
}

public async getProjects(): Promise<Project[]> {
Expand All @@ -144,7 +144,7 @@ export class ProjectManagementHttpApiV0 {
return response.json().then((body) => body as Project[]);
}
const err = await response.json();
throw new APIError(response.status, err.error);
throw new APIError(err.error, response.status);
}

public async getBucketRollup(projectID: UUID, bucket: string, since: Time, before: Time): Promise<BucketUsageRollup> {
Expand All @@ -159,7 +159,7 @@ export class ProjectManagementHttpApiV0 {
return response.json().then((body) => body as BucketUsageRollup);
}
const err = await response.json();
throw new APIError(response.status, err.error);
throw new APIError(err.error, response.status);
}

public async getBucketRollups(projectID: UUID, since: Time, before: Time): Promise<BucketUsageRollup[]> {
Expand All @@ -173,7 +173,7 @@ export class ProjectManagementHttpApiV0 {
return response.json().then((body) => body as BucketUsageRollup[]);
}
const err = await response.json();
throw new APIError(response.status, err.error);
throw new APIError(err.error, response.status);
}

public async getAPIKeys(projectID: UUID, search: string, limit: number, page: number, order: number, orderDirection: number): Promise<APIKeyPage> {
Expand All @@ -189,7 +189,7 @@ export class ProjectManagementHttpApiV0 {
return response.json().then((body) => body as APIKeyPage);
}
const err = await response.json();
throw new APIError(response.status, err.error);
throw new APIError(err.error, response.status);
}
}

Expand All @@ -204,7 +204,7 @@ export class APIKeyManagementHttpApiV0 {
return response.json().then((body) => body as CreateAPIKeyResponse);
}
const err = await response.json();
throw new APIError(response.status, err.error);
throw new APIError(err.error, response.status);
}

public async deleteAPIKey(id: UUID): Promise<void> {
Expand All @@ -214,7 +214,7 @@ export class APIKeyManagementHttpApiV0 {
return;
}
const err = await response.json();
throw new APIError(response.status, err.error);
throw new APIError(err.error, response.status);
}
}

Expand All @@ -229,6 +229,6 @@ export class UserManagementHttpApiV0 {
return response.json().then((body) => body as ResponseUser);
}
const err = await response.json();
throw new APIError(response.status, err.error);
throw new APIError(err.error, response.status);
}
}

0 comments on commit 48877c0

Please sign in to comment.