Skip to content

Instantly share code, notes, and snippets.

View tianhaoz95's full-sized avatar

Tianhao Zhou tianhaoz95

View GitHub Profile
@tianhaoz95
tianhaoz95 / install_ts_mocha.ts
Last active November 7, 2019 06:46
install ts mocha
/**
* First we need to install mocha, chai and their types with command:
* npm install --save-dev @types/mocha @types/chai mocha chai
*/
import "mocha";
describe("test iwfp api", () => {
it("hello world no crash", () => {
// run some tests here
@tianhaoz95
tianhaoz95 / test_fb_func_prep.ts
Created November 8, 2019 05:34
test fb func prep
import funcTest from "firebase-functions-test";
describe("test iwfp api", () => {
let iwfpapi, adminInitStub;
const tester = funcTest();
before(async () => {
adminInitStub = sinon.stub(admin, "initializeApp");
iwfpapi = await import("../src/index");
});
@tianhaoz95
tianhaoz95 / write_fb_tests.ts
Created November 8, 2019 05:40
write fb tests
describe("test iwfp api", () => {
// prepare code shown in the last section.
it("hello world no crash", () => {
const wrapped = tester.wrap(iwfpapi.helloWorld);
wrapped(null);
});
});
@tianhaoz95
tianhaoz95 / fb_test_package.json
Created November 8, 2019 05:47
fb test package.json
{
"name": "example test",
"scripts": {
"test": "mocha -r ts-node/register --reporter spec test/**/*.ts"
}
}
// To run the tests, just use: npm test
@tianhaoz95
tianhaoz95 / main.ts
Created November 22, 2019 04:43
the overall structure of readme linter
export async function lintWorkspace() {
try {
const workspaceDir = util.getGitHubWorkspace();
const workspaceFiles: string[] = util.getLintFileList(workspaceDir);
const reportsMetadata = new Array();
for (const workspaceFile of workspaceFiles) {
if (util.isReadmeFilename(workspaceFile)) {
const readmeFileContent = util.readFileContent(workspaceFile);
const relativePath = path.relative(workspaceDir, workspaceFile);
const reportEntry = {
@tianhaoz95
tianhaoz95 / in_app_api.ts
Last active January 15, 2020 20:48
In app version of Firebase API
export const internalAwesomeAPI = functions.https.onCall(async (data, fbContext) => {
const context: FunctionContext = provider.fbContext2context(fbContext);
const intermediateRequest: IntermediateRequestInterface = parseIntermediateRequest(data);
await awesomeHandler(intermediateRequest, context);
});
@tianhaoz95
tianhaoz95 / external_api.ts
Last active January 15, 2020 21:26
External version of Firebase API
export const externalAwesomeAPI = functions.https.onRequest(async (req, res) => {
if (req.method !== "POST") {
return res.sendStatus(403);
}
const context: FunctionContext = await token2context(req.body.token);
const intermediateRequest: IntermediateRequestInterface = parseIntermediateRequest(req.body);
try {
await awesomeHandler(intermediateRequest, context);
return res.sendStatus(200);
} catch (err) {
@tianhaoz95
tianhaoz95 / api_handler.ts
Last active January 15, 2020 23:31
The handler for unified Firebase APIs
async function awesomeHandler(req: any, context: FunctionContext) {
if (context.authenticated) {
const userUid: string = context.uid;
// Here goes your API implementation
} else {
throw UnauthenticatedUserError;
}
}
@tianhaoz95
tianhaoz95 / func_context_def.ts
Created January 15, 2020 23:34
Declaration of function context
export interface FunctionContext {
authenticated: boolean;
uid: string;
}
@tianhaoz95
tianhaoz95 / token2context.ts
Last active January 15, 2020 23:47
Token to context Firebase implementation
async token2context(token: string): Promise<FunctionContext> {
const context: FunctionContext = { authenticated: false, uid: "na" };
try {
const verifyResult = await this.auth.verifyIdToken(token);
context.authenticated = true;
context.uid = verifyResult.uid;
} catch (err) {
console.log(err);
}
return context;