Skip to content

Commit

Permalink
Regenerate examples (latest TypeScript example) (#207)
Browse files Browse the repository at this point in the history
Follow-up for #202
  • Loading branch information
VojtechVitek committed Jun 5, 2023
1 parent d31c49f commit bdcd995
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
35 changes: 21 additions & 14 deletions _examples/hello-webrpc-ts/webapp/src/client.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,37 +87,45 @@ export class ExampleService implements ExampleService {
return this.fetch(
this.url('Ping'),
createHTTPRequest({}, headers)
).then((res) => {
).then((res) => {
return buildResponse(res).then(_data => {
return {
status: <boolean>(_data.status)
status: <boolean>(_data.status),
}
})
}, (error) => {
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
})
}

getUser = (args: GetUserArgs, headers?: object): Promise<GetUserReturn> => {
return this.fetch(
this.url('GetUser'),
createHTTPRequest(args, headers)).then((res) => {
createHTTPRequest(args, headers)
).then((res) => {
return buildResponse(res).then(_data => {
return {
user: <User>(_data.user)
user: <User>(_data.user),
}
})
}, (error) => {
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
})
}

findUsers = (args: FindUsersArgs, headers?: object): Promise<FindUsersReturn> => {
return this.fetch(
this.url('FindUsers'),
createHTTPRequest(args, headers)).then((res) => {
createHTTPRequest(args, headers)
).then((res) => {
return buildResponse(res).then(_data => {
return {
page: <Page>(_data.page),
users: <Array<User>>(_data.users)
page: <Page>(_data.page),
users: <Array<User>>(_data.users),
}
})
}, (error) => {
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
})
}

Expand All @@ -136,16 +144,15 @@ const buildResponse = (res: Response): Promise<any> => {
let data
try {
data = JSON.parse(text)
} catch(e) {
WebrpcBadResponseError.throwError({
} catch(error) {
throw WebrpcBadResponseError.new({
status: res.status,
cause: `JSON.parse(): ${e.message}: response text: ${text}`},
cause: `JSON.parse(): ${error.message || ''}: response text: ${text}`},
)
}
if (!res.ok) {
const code: number = (typeof data.code === 'number') ? data.code : 0
const err = webrpcErrorByCode[code] || WebrpcError
err.throwError(data)
throw (webrpcErrorByCode[code] || WebrpcError).new(data)
}
return data
})
Expand Down Expand Up @@ -176,8 +183,8 @@ export class WebrpcError extends Error {
Object.setPrototypeOf(this, WebrpcError.prototype)
}

static throwError(payload: any) {
throw new this(payload.error, payload.code, payload.message || payload.msg, payload.status, payload.cause)
static new(payload: any): WebrpcError {
return new this(payload.error, payload.code, payload.message || payload.msg, payload.status, payload.cause)
}
}

Expand Down
29 changes: 16 additions & 13 deletions _examples/node-ts/webapp/client.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,28 @@ export class ExampleService implements ExampleService {
return this.fetch(
this.url('Ping'),
createHTTPRequest({}, headers)
).then((res) => {
).then((res) => {
return buildResponse(res).then(_data => {
return {
}
return {}
})
}, (error) => {
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
})
}

getUser = (args: GetUserArgs, headers?: object): Promise<GetUserReturn> => {
return this.fetch(
this.url('GetUser'),
createHTTPRequest(args, headers)).then((res) => {
createHTTPRequest(args, headers)
).then((res) => {
return buildResponse(res).then(_data => {
return {
code: <number>(_data.code),
user: <User>(_data.user)
code: <number>(_data.code),
user: <User>(_data.user),
}
})
}, (error) => {
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
})
}

Expand All @@ -114,16 +118,15 @@ const buildResponse = (res: Response): Promise<any> => {
let data
try {
data = JSON.parse(text)
} catch(e) {
WebrpcBadResponseError.throwError({
} catch(error) {
throw WebrpcBadResponseError.new({
status: res.status,
cause: `JSON.parse(): ${e.message}: response text: ${text}`},
cause: `JSON.parse(): ${error.message || ''}: response text: ${text}`},
)
}
if (!res.ok) {
const code: number = (typeof data.code === 'number') ? data.code : 0
const err = webrpcErrorByCode[code] || WebrpcError
err.throwError(data)
throw (webrpcErrorByCode[code] || WebrpcError).new(data)
}
return data
})
Expand Down Expand Up @@ -154,8 +157,8 @@ export class WebrpcError extends Error {
Object.setPrototypeOf(this, WebrpcError.prototype)
}

static throwError(payload: any) {
throw new this(payload.error, payload.code, payload.message || payload.msg, payload.status, payload.cause)
static new(payload: any): WebrpcError {
return new this(payload.error, payload.code, payload.message || payload.msg, payload.status, payload.cause)
}
}

Expand Down

0 comments on commit bdcd995

Please sign in to comment.