Skip to content

Commit

Permalink
tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
lukealvoeiro committed Feb 3, 2024
1 parent dbc1266 commit 835e7fe
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Observable, map, of } from "rxjs";
import { DashStateClientImpl, DashUserSettingsState, DashStateServiceName } from "./example";

interface Rpc {
request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>;
clientStreamingRequest(service: string, method: string, data: Observable<Uint8Array>): Promise<Uint8Array>;
serverStreamingRequest(service: string, method: string, data: Uint8Array): Observable<Uint8Array>;
bidirectionalStreamingRequest(service: string, method: string, data: Observable<Uint8Array>): Observable<Uint8Array>;
beforeRequest?<T extends { [k in keyof T]: unknown }>(service: string, method: string, request: T): void;
afterResponse?<T extends { [k in keyof T]: unknown }>(service: string, method: string, response: T): void;
handleError?(service: string, method: string, error: Error): Error;
}

describe("before-after-request-streaming", () => {
const reqData = {
email: "[email protected]",
urls: undefined,
flashes: [{ msg: "dun dun dun dun", type: 0 }],
};
const resData = {
email: "[email protected]",
urls: undefined,
flashes: [{ msg: "dun dun dun dun", type: 0 }],
};
let rpc: Rpc = {
request: jest.fn(() => Promise.resolve(new Uint8Array([21]))),
clientStreamingRequest: jest.fn(() => Promise.resolve(new Uint8Array([21]))),
serverStreamingRequest: jest.fn(() => of(new Uint8Array([21]))),
bidirectionalStreamingRequest: (service: string, method: string, data: Observable<Uint8Array>) => {
return data.pipe(map((data) => new Uint8Array([21])));
},
};
let client = new DashStateClientImpl(rpc);
const beforeRequest = jest.fn();
const afterResponse = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
jest.spyOn(DashUserSettingsState, "decode").mockReturnValue(resData);
});

it("compiles", () => {
const service = new DashStateClientImpl(rpc);
expect(service).not.toBeUndefined();
});

it("performs function before request if specified", () => {
const req = DashUserSettingsState.create(reqData);
client = new DashStateClientImpl({ ...rpc, beforeRequest: beforeRequest });
const reqObservable = of(req);
client.ChangeUserSettingsStream(reqObservable).subscribe();
expect(beforeRequest).toHaveBeenCalledWith(
DashStateServiceName,
"ChangeUserSettingsStream",
DashUserSettingsState.encode(req).finish(),
);
});

it("performs function after request if specified", async () => {
const req = DashUserSettingsState.create(reqData);
client = new DashStateClientImpl({ ...rpc, afterResponse: afterResponse });
const reqObservable = of(req);
client.ChangeUserSettingsStream(reqObservable).subscribe();
expect(afterResponse).toHaveBeenCalledWith(DashStateServiceName, "ChangeUserSettingsStream", resData);
});

it("doesn't perform function before or after request if they are not specified", async () => {
const req = DashUserSettingsState.create(reqData);
client = new DashStateClientImpl({ ...rpc });
await client.ChangeUserSettingsStream(of(req));
expect(beforeRequest).not.toHaveBeenCalled();
expect(afterResponse).not.toHaveBeenCalled();
});
});
223 changes: 122 additions & 101 deletions integration/before-after-request-streaming/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ export interface DashAPICredsDeleteReq {
id: string;
}

export interface Empty {
}
export interface Empty {}

function createBaseDashFlash(): DashFlash {
return { msg: "", type: 0 };
Expand Down Expand Up @@ -251,9 +250,10 @@ 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,22 +756,24 @@ 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 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 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 @@ -780,31 +782,37 @@ 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) => {
const response = DashUserSettingsState.decode(_m0.Reader.create(data));
if (this.rpc.afterResponse) {
this.rpc.afterResponse(this.service, "ActiveUserSettingsStream", response);
}
return response;
}));
return result.pipe(
map((data) => {
const response = DashUserSettingsState.decode(_m0.Reader.create(data));
if (this.rpc.afterResponse) {
this.rpc.afterResponse(this.service, "ActiveUserSettingsStream", response);
}
return response;
}),
);
}

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) => {
const response = DashUserSettingsState.decode(_m0.Reader.create(data));
if (this.rpc.afterResponse) {
this.rpc.afterResponse(this.service, "ChangeUserSettingsStream", response);
}
return response;
}));
return result.pipe(
map((data) => {
const response = DashUserSettingsState.decode(_m0.Reader.create(data));
if (this.rpc.afterResponse) {
this.rpc.afterResponse(this.service, "ChangeUserSettingsStream", response);
}
return response;
}),
);
}
}

Expand Down Expand Up @@ -871,22 +879,24 @@ 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 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 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 @@ -895,22 +905,24 @@ 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 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 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 @@ -919,22 +931,24 @@ 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 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 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 @@ -943,22 +957,24 @@ 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 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 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 @@ -1019,14 +1035,19 @@ 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

0 comments on commit 835e7fe

Please sign in to comment.