Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove 'request' from 'functions:shell' #5808

Merged
merged 21 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
format
  • Loading branch information
joehan committed Nov 13, 2023
commit bb27a88ec61045e4b44721b1ed83e7694dae0ec5
1 change: 0 additions & 1 deletion src/emulator/functionsEmulatorRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { CloudFunction, DeploymentOptions, https } from "firebase-functions";
import * as express from "express";
import * as path from "path";
import * as admin from "firebase-admin";
import { RemoteConfigTemplate } from "firebase-admin/remote-config";
import * as bodyParser from "body-parser";
import { pathToFileURL, URL } from "url";
import * as _ from "lodash";
Expand Down
50 changes: 35 additions & 15 deletions src/localFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default class LocalFunction {
method: (path: string, options?: ClientVerbOptions) => Promise<ClientResponse<any>>
): verbFn => {
return async (pathOrOptions?: string | HttpsOptions, options?: HttpsOptions) => {
const { path, opts } = this.extractArgs(pathOrOptions, options);
const { path, opts } = this.extractArgs(pathOrOptions, options);
await method(path, toClientVerbOptions(opts))
.then((res) => {
this.requestCallBack(undefined, res, res.body);
Expand All @@ -95,22 +95,43 @@ export default class LocalFunction {
};
};
const shim = verbWithReqBodyFactory((path: string, json?: any, opts?: ClientVerbOptions) => {
const req = Object.assign(opts || {} , {path: path, body: json, method: opts?.method || "GET"});
const req = Object.assign(opts || {}, {
path: path,
body: json,
method: opts?.method || "GET",
});
return callClient.request(req);
});
const verbs: verbMethods = {
post: verbWithReqBodyFactory((path: string, json?: any, opts?: ClientVerbOptions) => callClient.post(path, json, opts)),
put: verbWithReqBodyFactory((path: string, json?: any, opts?: ClientVerbOptions) => callClient.put(path, json, opts)),
patch: verbWithReqBodyFactory((path: string, json?: any, opts?: ClientVerbOptions) => callClient.patch(path, json, opts)),
get: verbWithoutReqBodyFactory((path: string, opts?: ClientVerbOptions) => callClient.get(path, opts)),
del: verbWithoutReqBodyFactory((path: string, opts?: ClientVerbOptions) => callClient.delete(path, opts)),
delete: verbWithoutReqBodyFactory((path: string, opts?: ClientVerbOptions) => callClient.delete(path, opts)),
options: verbWithoutReqBodyFactory((path: string, opts?: ClientVerbOptions) => callClient.options(path, opts)),
post: verbWithReqBodyFactory((path: string, json?: any, opts?: ClientVerbOptions) =>
callClient.post(path, json, opts)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: can this be:

callClient.post.bind(callClient))

instead? (to save some characters)

),
put: verbWithReqBodyFactory((path: string, json?: any, opts?: ClientVerbOptions) =>
callClient.put(path, json, opts)
),
patch: verbWithReqBodyFactory((path: string, json?: any, opts?: ClientVerbOptions) =>
callClient.patch(path, json, opts)
),
get: verbWithoutReqBodyFactory((path: string, opts?: ClientVerbOptions) =>
callClient.get(path, opts)
),
del: verbWithoutReqBodyFactory((path: string, opts?: ClientVerbOptions) =>
callClient.delete(path, opts)
),
delete: verbWithoutReqBodyFactory((path: string, opts?: ClientVerbOptions) =>
callClient.delete(path, opts)
),
options: verbWithoutReqBodyFactory((path: string, opts?: ClientVerbOptions) =>
callClient.options(path, opts)
),
};
return Object.assign(shim, verbs);
}

private extractArgs(pathOrOptions?: string | HttpsOptions, options?: HttpsOptions): {path: string, opts: HttpsOptions} {
private extractArgs(
pathOrOptions?: string | HttpsOptions,
options?: HttpsOptions
): { path: string; opts: HttpsOptions } {
let path = "/";
let opts: HttpsOptions = {};
// Just path
Expand All @@ -121,15 +142,14 @@ export default class LocalFunction {
throw new Error(
joehan marked this conversation as resolved.
Show resolved Hide resolved
`Expected args of type string, HttpsOptions, got ${typeof pathOrOptions}, ${typeof options}`
);
} // path and options
} // path and options
else if (!!pathOrOptions && !!options && typeof pathOrOptions === "string") {
path = pathOrOptions;
opts = options;
} else if (!!pathOrOptions && typeof pathOrOptions !== "string") {
opts = pathOrOptions;
}
else if (!!pathOrOptions && typeof pathOrOptions != "string") {
opts = pathOrOptions
}
return {path, opts}
return { path, opts };
}

constructAuth(auth?: EventOptions["auth"], authType?: AuthType): AuthMode {
Expand Down
Loading