diff --git a/integration/before-after-request-streaming/example.ts b/integration/before-after-request-streaming/example.ts index e884224a8..97b9973b2 100644 --- a/integration/before-after-request-streaming/example.ts +++ b/integration/before-after-request-streaming/example.ts @@ -91,7 +91,8 @@ export interface DashAPICredsDeleteReq { id: string; } -export interface Empty {} +export interface Empty { +} function createBaseDashFlash(): DashFlash { return { msg: "", type: 0 }; @@ -250,10 +251,9 @@ export const DashUserSettingsState = { fromPartial, I>>(object: I): DashUserSettingsState { const message = createBaseDashUserSettingsState(); message.email = object.email ?? ""; - message.urls = - object.urls !== undefined && object.urls !== null - ? DashUserSettingsState_URLs.fromPartial(object.urls) - : undefined; + message.urls = (object.urls !== undefined && object.urls !== null) + ? DashUserSettingsState_URLs.fromPartial(object.urls) + : undefined; message.flashes = object.flashes?.map((e) => DashFlash.fromPartial(e)) || []; return message; }, @@ -756,24 +756,22 @@ export class DashStateClientImpl implements DashState { this.rpc.beforeRequest(this.service, "UserSettings", request); } const promise = this.rpc.request(this.service, "UserSettings", data); - return promise - .then((data) => { - try { - const response = DashUserSettingsState.decode(_m0.Reader.create(data)); - if (this.rpc.afterResponse) { - this.rpc.afterResponse(this.service, "UserSettings", response); - } - return response; - } catch (error) { - return Promise.reject(error); - } - }) - .catch((error) => { - if (this.rpc.handleError) { - return Promise.reject(this.rpc.handleError(this.service, "UserSettings", error)); + return promise.then((data) => { + try { + const response = DashUserSettingsState.decode(_m0.Reader.create(data)); + if (this.rpc.afterResponse) { + this.rpc.afterResponse(this.service, "UserSettings", response); } + return response; + } catch (error) { return Promise.reject(error); - }); + } + }).catch((error) => { + if (this.rpc.handleError) { + return Promise.reject(this.rpc.handleError(this.service, "UserSettings", error)); + } + return Promise.reject(error); + }); } ActiveUserSettingsStream(request: Empty): Observable { @@ -782,37 +780,39 @@ export class DashStateClientImpl implements DashState { this.rpc.beforeRequest(this.service, "ActiveUserSettingsStream", request); } const result = this.rpc.serverStreamingRequest(this.service, "ActiveUserSettingsStream", data); - return result.pipe( - map((data) => { + return result.pipe(map((data) => { + try { const response = DashUserSettingsState.decode(_m0.Reader.create(data)); if (this.rpc.afterResponse) { this.rpc.afterResponse(this.service, "ActiveUserSettingsStream", response); } return response; - }), - ); + } catch (error) { + throw error; + } + })); } ChangeUserSettingsStream(request: Observable): Observable { - const data = request.pipe( - map((request) => { - const encodedRequest = DashUserSettingsState.encode(request).finish(); - if (this.rpc.beforeRequest) { - this.rpc.beforeRequest(this.service, "ChangeUserSettingsStream", encodedRequest); - } - return encodedRequest; - }), - ); + const data = request.pipe(map((request) => { + const encodedRequest = DashUserSettingsState.encode(request).finish(); + if (this.rpc.beforeRequest) { + this.rpc.beforeRequest(this.service, "ChangeUserSettingsStream", encodedRequest); + } + return encodedRequest; + })); const result = this.rpc.bidirectionalStreamingRequest(this.service, "ChangeUserSettingsStream", data); - return result.pipe( - map((data) => { + return result.pipe(map((data) => { + try { const response = DashUserSettingsState.decode(_m0.Reader.create(data)); if (this.rpc.afterResponse) { this.rpc.afterResponse(this.service, "ChangeUserSettingsStream", response); } return response; - }), - ); + } catch (error) { + throw error; + } + })); } } @@ -879,24 +879,22 @@ export class DashAPICredsClientImpl implements DashAPICreds { this.rpc.beforeRequest(this.service, "Create", request); } const promise = this.rpc.request(this.service, "Create", data); - return promise - .then((data) => { - try { - const response = DashCred.decode(_m0.Reader.create(data)); - if (this.rpc.afterResponse) { - this.rpc.afterResponse(this.service, "Create", response); - } - return response; - } catch (error) { - return Promise.reject(error); - } - }) - .catch((error) => { - if (this.rpc.handleError) { - return Promise.reject(this.rpc.handleError(this.service, "Create", error)); + return promise.then((data) => { + try { + const response = DashCred.decode(_m0.Reader.create(data)); + if (this.rpc.afterResponse) { + this.rpc.afterResponse(this.service, "Create", response); } + return response; + } catch (error) { return Promise.reject(error); - }); + } + }).catch((error) => { + if (this.rpc.handleError) { + return Promise.reject(this.rpc.handleError(this.service, "Create", error)); + } + return Promise.reject(error); + }); } Update(request: DashAPICredsUpdateReq): Promise { @@ -905,24 +903,22 @@ export class DashAPICredsClientImpl implements DashAPICreds { this.rpc.beforeRequest(this.service, "Update", request); } const promise = this.rpc.request(this.service, "Update", data); - return promise - .then((data) => { - try { - const response = DashCred.decode(_m0.Reader.create(data)); - if (this.rpc.afterResponse) { - this.rpc.afterResponse(this.service, "Update", response); - } - return response; - } catch (error) { - return Promise.reject(error); - } - }) - .catch((error) => { - if (this.rpc.handleError) { - return Promise.reject(this.rpc.handleError(this.service, "Update", error)); + return promise.then((data) => { + try { + const response = DashCred.decode(_m0.Reader.create(data)); + if (this.rpc.afterResponse) { + this.rpc.afterResponse(this.service, "Update", response); } + return response; + } catch (error) { return Promise.reject(error); - }); + } + }).catch((error) => { + if (this.rpc.handleError) { + return Promise.reject(this.rpc.handleError(this.service, "Update", error)); + } + return Promise.reject(error); + }); } Delete(request: DashAPICredsDeleteReq): Promise { @@ -931,24 +927,22 @@ export class DashAPICredsClientImpl implements DashAPICreds { this.rpc.beforeRequest(this.service, "Delete", request); } const promise = this.rpc.request(this.service, "Delete", data); - return promise - .then((data) => { - try { - const response = DashCred.decode(_m0.Reader.create(data)); - if (this.rpc.afterResponse) { - this.rpc.afterResponse(this.service, "Delete", response); - } - return response; - } catch (error) { - return Promise.reject(error); - } - }) - .catch((error) => { - if (this.rpc.handleError) { - return Promise.reject(this.rpc.handleError(this.service, "Delete", error)); + return promise.then((data) => { + try { + const response = DashCred.decode(_m0.Reader.create(data)); + if (this.rpc.afterResponse) { + this.rpc.afterResponse(this.service, "Delete", response); } + return response; + } catch (error) { return Promise.reject(error); - }); + } + }).catch((error) => { + if (this.rpc.handleError) { + return Promise.reject(this.rpc.handleError(this.service, "Delete", error)); + } + return Promise.reject(error); + }); } Uppercase(request: StringValue): Promise { @@ -957,24 +951,22 @@ export class DashAPICredsClientImpl implements DashAPICreds { this.rpc.beforeRequest(this.service, "Uppercase", request); } const promise = this.rpc.request(this.service, "Uppercase", data); - return promise - .then((data) => { - try { - const response = StringValue.decode(_m0.Reader.create(data)); - if (this.rpc.afterResponse) { - this.rpc.afterResponse(this.service, "Uppercase", response); - } - return response; - } catch (error) { - return Promise.reject(error); - } - }) - .catch((error) => { - if (this.rpc.handleError) { - return Promise.reject(this.rpc.handleError(this.service, "Uppercase", error)); + return promise.then((data) => { + try { + const response = StringValue.decode(_m0.Reader.create(data)); + if (this.rpc.afterResponse) { + this.rpc.afterResponse(this.service, "Uppercase", response); } + return response; + } catch (error) { return Promise.reject(error); - }); + } + }).catch((error) => { + if (this.rpc.handleError) { + return Promise.reject(this.rpc.handleError(this.service, "Uppercase", error)); + } + return Promise.reject(error); + }); } } @@ -1035,19 +1027,14 @@ interface Rpc { type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; -export type DeepPartial = T extends Builtin - ? T - : T extends globalThis.Array - ? globalThis.Array> - : T extends ReadonlyArray - ? ReadonlyArray> - : T extends {} - ? { [K in keyof T]?: DeepPartial } +export type DeepPartial = T extends Builtin ? T + : T extends globalThis.Array ? globalThis.Array> + : T extends ReadonlyArray ? ReadonlyArray> + : T extends {} ? { [K in keyof T]?: DeepPartial } : Partial; type KeysOfUnion = T extends T ? keyof T : never; -export type Exact = P extends Builtin - ? P +export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function isSet(value: any): boolean { diff --git a/src/generate-services.ts b/src/generate-services.ts index 924c33532..42fca4402 100644 --- a/src/generate-services.ts +++ b/src/generate-services.ts @@ -10,6 +10,7 @@ import { observableType, } from "./types"; import { + arrowFunction, assertInstanceOf, FormattedMethodDescriptor, impFile, @@ -214,27 +215,29 @@ function createDefaultServiceReturn( ): Code { const { options } = ctx; const rawOutputType = responseType(ctx, methodDesc, { keepValueType: true }); - let returnStatement = code`data => ${decode}`; - if (options.rpcAfterResponse) { - returnStatement = code`data => { ${decode} }`; - } + const returnStatement = arrowFunction("data", decode, !options.rpcAfterResponse); + if (options.returnObservable || methodDesc.serverStreaming) { if (options.useAsyncIterable) { return code`${rawOutputType}.decodeTransform(result)`; } else { + if (errorHandler) { + const tc = arrowFunction("data", tryCatchBlock(decode, code`throw error`), !options.rpcAfterResponse); + return code`result.pipe(${imp("map@rxjs/operators")}(${tc}))`; + } return code`result.pipe(${imp("map@rxjs/operators")}(${returnStatement}))`; } } if (errorHandler) { - let tryBlock = decode; if (!options.rpcAfterResponse) { - tryBlock = code`return ${decode}`; + decode = code`return ${decode}`; } - return code`promise.then(data => { ${tryCatchBlock( - tryBlock, - code`return Promise.reject(error);`, - )}}).catch((error) => { ${errorHandler} })`; + return code`promise.then(${arrowFunction( + "data", + tryCatchBlock(decode, code`return Promise.reject(error);`), + false, + )}).catch(${arrowFunction("error", errorHandler, false)})`; } return code`promise.then(${returnStatement})`; } diff --git a/src/utils.ts b/src/utils.ts index b36c89e86..9983c8f9e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -296,3 +296,10 @@ export function tryCatchBlock(tryBlock: Code | string, handleErrorBlock: Code | ${handleErrorBlock} }`; } + +export function arrowFunction(params: string, body: Code | string, isOneLine: boolean = true): Code { + if (isOneLine) { + return code`(${params}) => ${body}`; + } + return code`(${params}) => { ${body} }`; +}