private/apigen: Fix TS generator to align with linter

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
This commit is contained in:
Ivan Fraixedes 2023-11-02 13:46:35 +01:00
parent f6e357be52
commit 48877c05cc
No known key found for this signature in database
GPG Key ID: FB6101AFB5CB5AD5
6 changed files with 45 additions and 40 deletions

View File

@ -39,7 +39,7 @@ export class Metadata {
tags?: string[][]; tags?: string[][];
} }
export class UsersCreateRequestItem { export class UsersGetResponseItem {
name: string; name: string;
surname: string; surname: string;
email: string; email: string;
@ -54,9 +54,9 @@ export type DocsGetResponse = Array<DocsGetResponseItem>
export type DocsGetResponseItemLastRetrievals = Array<DocsGetResponseItemLastRetrievalsItem> 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 { class APIError extends Error {
constructor( constructor(
@ -87,43 +87,43 @@ export class DocumentsHttpApiV0 {
} }
public async get(): Promise<DocsGetResponse> { public async get(): Promise<DocsGetResponse> {
if (this.respStatusCode != 0) { if (this.respStatusCode !== 0) {
throw new APIError('mock error message: ' + this.respStatusCode, this.respStatusCode); 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> { 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); 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[]> { 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); 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[]> { 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); 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> { 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); 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;
} }
} }
@ -147,15 +147,15 @@ export class UsersHttpApiV0 {
} }
public async get(): Promise<UsersGetResponse> { public async get(): Promise<UsersGetResponse> {
if (this.respStatusCode != 0) { if (this.respStatusCode !== 0) {
throw new APIError('mock error message: ' + this.respStatusCode, this.respStatusCode); 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> { 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); throw new APIError('mock error message: ' + this.respStatusCode, this.respStatusCode);
} }

View File

@ -41,7 +41,7 @@ export class Metadata {
tags?: string[][]; tags?: string[][];
} }
export class UsersGetResponseItem { export class UsersCreateRequestItem {
name: string; name: string;
surname: string; surname: string;
email: string; email: string;
@ -56,9 +56,9 @@ export type DocsGetResponse = Array<DocsGetResponseItem>
export type DocsGetResponseItemLastRetrievals = Array<DocsGetResponseItemLastRetrievalsItem> 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 { class APIError extends Error {
constructor( constructor(
@ -80,7 +80,7 @@ export class DocumentsHttpApiV0 {
return response.json().then((body) => body as DocsGetResponse); return response.json().then((body) => body as DocsGetResponse);
} }
const err = await response.json(); 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> { public async getOne(path: string): Promise<Document> {
@ -90,7 +90,7 @@ export class DocumentsHttpApiV0 {
return response.json().then((body) => body as Document); return response.json().then((body) => body as Document);
} }
const err = await response.json(); 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[]> { public async getTag(path: string, tagName: string): Promise<string[]> {
@ -100,7 +100,7 @@ export class DocumentsHttpApiV0 {
return response.json().then((body) => body as string[]); return response.json().then((body) => body as string[]);
} }
const err = await response.json(); 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[]> { public async getVersions(path: string): Promise<Version[]> {
@ -110,7 +110,7 @@ export class DocumentsHttpApiV0 {
return response.json().then((body) => body as Version[]); return response.json().then((body) => body as Version[]);
} }
const err = await response.json(); 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> { public async updateContent(request: DocsUpdateContentRequest, path: string, id: UUID, date: Time): Promise<DocsUpdateContentResponse> {
@ -123,7 +123,7 @@ export class DocumentsHttpApiV0 {
return response.json().then((body) => body as DocsUpdateContentResponse); return response.json().then((body) => body as DocsUpdateContentResponse);
} }
const err = await response.json(); const err = await response.json();
throw new APIError(err.error,response.status); throw new APIError(err.error, response.status);
} }
} }
@ -138,7 +138,7 @@ export class UsersHttpApiV0 {
return response.json().then((body) => body as UsersGetResponse); return response.json().then((body) => body as UsersGetResponse);
} }
const err = await response.json(); 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> { public async create(request: UsersCreateRequest): Promise<void> {
@ -148,6 +148,6 @@ export class UsersHttpApiV0 {
return; return;
} }
const err = await response.json(); const err = await response.json();
throw new Error(err.error); throw new APIError(err.error, response.status);
} }
} }

View File

@ -133,7 +133,7 @@ func (f *tsGenFile) createAPIClient(group *EndpointGroup) {
f.pf("\t\t\t%s", returnStmt) f.pf("\t\t\t%s", returnStmt)
f.pf("\t\t}") f.pf("\t\t}")
f.pf("\t\tconst err = await response.json();") 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("\t}")
} }
f.pf("}") f.pf("}")

View File

@ -103,7 +103,7 @@ func (f *tsGenMockFile) createAPIClient(group *EndpointGroup) {
} }
f.pf("\tpublic async %s(%s): Promise<%s> {", method.TypeScriptName, funcArgs, returnType) 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\tthrow new APIError('mock error message: ' + this.respStatusCode, this.respStatusCode);")
f.pf("\t\t}") f.pf("\t\t}")
f.pf("") f.pf("")
@ -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 { } else {
f.pf("\t\treturn;") f.pf("\t\treturn;")
} }

View File

@ -144,7 +144,12 @@ func (types *Types) GenerateTypescriptDefinitions() string {
var out StringBuilder var out StringBuilder
pf := out.Writelnf pf := out.Writelnf
pf(types.getTypescriptImports()) {
i := types.getTypescriptImports()
if i != "" {
pf(i)
}
}
allTypes := types.All() allTypes := types.All()
namedTypes := mapToSlice(allTypes) namedTypes := mapToSlice(allTypes)

View File

@ -114,7 +114,7 @@ export class ProjectManagementHttpApiV0 {
return response.json().then((body) => body as Project); return response.json().then((body) => body as Project);
} }
const err = await response.json(); 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> { public async updateProject(request: UpsertProjectInfo, id: UUID): Promise<Project> {
@ -124,7 +124,7 @@ export class ProjectManagementHttpApiV0 {
return response.json().then((body) => body as Project); return response.json().then((body) => body as Project);
} }
const err = await response.json(); 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> { public async deleteProject(id: UUID): Promise<void> {
@ -134,7 +134,7 @@ export class ProjectManagementHttpApiV0 {
return; return;
} }
const err = await response.json(); const err = await response.json();
throw new APIError(response.status, err.error); throw new APIError(err.error, response.status);
} }
public async getProjects(): Promise<Project[]> { public async getProjects(): Promise<Project[]> {
@ -144,7 +144,7 @@ export class ProjectManagementHttpApiV0 {
return response.json().then((body) => body as Project[]); return response.json().then((body) => body as Project[]);
} }
const err = await response.json(); 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> { public async getBucketRollup(projectID: UUID, bucket: string, since: Time, before: Time): Promise<BucketUsageRollup> {
@ -159,7 +159,7 @@ export class ProjectManagementHttpApiV0 {
return response.json().then((body) => body as BucketUsageRollup); return response.json().then((body) => body as BucketUsageRollup);
} }
const err = await response.json(); 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[]> { public async getBucketRollups(projectID: UUID, since: Time, before: Time): Promise<BucketUsageRollup[]> {
@ -173,7 +173,7 @@ export class ProjectManagementHttpApiV0 {
return response.json().then((body) => body as BucketUsageRollup[]); return response.json().then((body) => body as BucketUsageRollup[]);
} }
const err = await response.json(); 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> { public async getAPIKeys(projectID: UUID, search: string, limit: number, page: number, order: number, orderDirection: number): Promise<APIKeyPage> {
@ -189,7 +189,7 @@ export class ProjectManagementHttpApiV0 {
return response.json().then((body) => body as APIKeyPage); return response.json().then((body) => body as APIKeyPage);
} }
const err = await response.json(); const err = await response.json();
throw new APIError(response.status, err.error); throw new APIError(err.error, response.status);
} }
} }
@ -204,7 +204,7 @@ export class APIKeyManagementHttpApiV0 {
return response.json().then((body) => body as CreateAPIKeyResponse); return response.json().then((body) => body as CreateAPIKeyResponse);
} }
const err = await response.json(); 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> { public async deleteAPIKey(id: UUID): Promise<void> {
@ -214,7 +214,7 @@ export class APIKeyManagementHttpApiV0 {
return; return;
} }
const err = await response.json(); const err = await response.json();
throw new APIError(response.status, err.error); throw new APIError(err.error, response.status);
} }
} }
@ -229,6 +229,6 @@ export class UserManagementHttpApiV0 {
return response.json().then((body) => body as ResponseUser); return response.json().then((body) => body as ResponseUser);
} }
const err = await response.json(); const err = await response.json();
throw new APIError(response.status, err.error); throw new APIError(err.error, response.status);
} }
} }