Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lukealvoeiro committed Feb 3, 2024
1 parent 835e7fe commit 4710603
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 122 deletions.
211 changes: 99 additions & 112 deletions integration/before-after-request-streaming/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ export interface DashAPICredsDeleteReq {
id: string;
}

export interface Empty {}
export interface Empty {
}

function createBaseDashFlash(): DashFlash {
return { msg: "", type: 0 };
Expand Down Expand Up @@ -250,10 +251,9 @@ export const DashUserSettingsState = {
fromPartial<I extends Exact<DeepPartial<DashUserSettingsState>, 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;
},
Expand Down Expand Up @@ -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<DashUserSettingsState> {
Expand All @@ -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<DashUserSettingsState>): Observable<DashUserSettingsState> {
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;
}
}));
}
}

Expand Down Expand Up @@ -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<DashCred> {
Expand All @@ -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<DashCred> {
Expand All @@ -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<StringValue> {
Expand All @@ -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);
});
}
}

Expand Down Expand Up @@ -1035,19 +1027,14 @@ interface Rpc {

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

export type DeepPartial<T> = T extends Builtin
? T
: T extends globalThis.Array<infer U>
? globalThis.Array<DeepPartial<U>>
: T extends ReadonlyArray<infer U>
? ReadonlyArray<DeepPartial<U>>
: T extends {}
? { [K in keyof T]?: DeepPartial<T[K]> }
export type DeepPartial<T> = T extends Builtin ? T
: T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>>
: T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>;

type KeysOfUnion<T> = T extends T ? keyof T : never;
export type Exact<P, I extends P> = P extends Builtin
? P
export type Exact<P, I extends P> = P extends Builtin ? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };

function isSet(value: any): boolean {
Expand Down
23 changes: 13 additions & 10 deletions src/generate-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
observableType,
} from "./types";
import {
arrowFunction,
assertInstanceOf,
FormattedMethodDescriptor,
impFile,
Expand Down Expand Up @@ -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})`;
}
Expand Down
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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} }`;
}

0 comments on commit 4710603

Please sign in to comment.