Skip to content
This repository was archived by the owner on Nov 18, 2025. It is now read-only.

Commit 5429ad9

Browse files
feat: allow to pass just one file path to loadProto (#543)
1 parent 7c2d11d commit 5429ad9

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/grpc.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,16 @@ export class GrpcClient {
168168
* when necessary.
169169
* @param {String} protoPath - The directory to search for the protofile.
170170
* @param {String|String[]} filename - The filename(s) of the proto(s) to be loaded.
171+
* If omitted, protoPath will be treated as a file path to load.
171172
* @return {Object<string, *>} The gRPC loaded result (the toplevel namespace
172173
* object).
173174
*/
174-
loadProto(protoPath: string, filename: string | string[]) {
175+
loadProto(protoPath: string, filename?: string | string[]) {
176+
if (!filename) {
177+
filename = path.basename(protoPath);
178+
protoPath = path.dirname(protoPath);
179+
}
180+
175181
if (Array.isArray(filename) && filename.length === 0) {
176182
return {};
177183
}

test/grpc.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@ describe('grpc', () => {
235235
);
236236
});
237237

238+
it('should load the test file using single parameter syntax', () => {
239+
const fullPath = path.join(TEST_PATH, TEST_FILE);
240+
// no-any disabled because if the accessed fields are non-existent, this
241+
// test will fail anyway.
242+
// tslint:disable-next-line:no-any
243+
const protos = grpcClient.loadProto(fullPath) as any;
244+
expect(protos.google.example.library.v1.LibraryService).to.be.a(
245+
'Function'
246+
);
247+
});
248+
238249
it('should load a common proto', () => {
239250
const nonExistentDir = path.join(__dirname, 'nonexistent', 'dir');
240251
const iamService = path.join('google', 'iam', 'v1', 'iam_policy.proto');

0 commit comments

Comments
 (0)