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

typescript please! #107

Open
jmagaram opened this issue Feb 24, 2021 · 5 comments
Open

typescript please! #107

jmagaram opened this issue Feb 24, 2021 · 5 comments

Comments

@jmagaram
Copy link

jmagaram commented Feb 24, 2021

Desperately needing samples in TypeScript. I'm trying to test functions now and don't have a clue what parameters are possible on Request and Response objects. Have Googled for hours and can't find what I need. Would love to see some really basic function tests in Mocha and Typescript against the emulator. Tests for...

HTTP
If function is supposed to return an uppercase version of what is passed in, check that
Function grabs parameters out of the query string
If function is supposed to return an error code, like forbidden, show that
If function is supposed to write to a document
Fail if not authenticated, return proper status code
Check claims, fail if not met

INTERNAL SERVER
On user create, create a document
On user delete, delete a document
On document changes, do something

===

Digging through your code a bit I see references to "core" and Express. Can I even get these types into my code? I'd like to strongly type my request and response objects. Where do I import these from?

===

Investigated some more and see it is Express web server under the covers? I'm going to assume that testing this in Typescript is going to be tough because there are so many possible properties on the Request object - headers and such - and so many ways to build the response and I'd need test helpers to construct these things in such a way that they'd type check. Also don't know how this interacts with the Auth stuff - do I get the token from the headers or from the Auth API. This is really beyond me - too much to learn - probably will just skip the unit testing. Can't do it without some getting started info from you with common cases in code.

@samtstern
Copy link
Contributor

@jmagaram thanks for the feedback! Yes you're right, all Node.js Google Cloud Functions (and therefore Firebase functions) which use HTTP triggers use Express for their request/response. That's why we don't have documentation on those types on our site, they're not actually part of our code.

By default there's no Authentication state included with a request to an HTTPS function. You can pass whatever headers you want.

If you want to use a more "standardized" approach to HTTPS functions check out Callable Functions:
https://firebase.google.com/docs/functions/callable

These functions have a more opinionated structure so:

  • Passing data in/out is easier
  • Authentication is seamlessly handled with Firebase Auth, no need to mess with headers
  • Returning an error is simple with the HttpsError class

I think you'll find things much better typed with callable functions, if you try them let me know what you think.

@jmagaram
Copy link
Author

Wrote one and called it from my app. Worked great. Haven't tried calling it from a unit test.

@samtstern
Copy link
Contributor

@jmagaram glad to hear you're making progress! Once you try unit testing please let me know if you think there are some patterns which need more explanation here.

@alexshikov
Copy link

Could anyone please share a short example of TypeScript unit-test for the onCall function? For some reason it neither easy to find an example nor write a working unit-test for the simplest function like:

import {onCall} from "firebase-functions/v2/https";
export const helloWorld = onCall((request) => {
  return "Hello!";
});

@alexshikov
Copy link

Alright, I believe I've found it:

import 'mocha';
import { expect } from 'chai';
import { helloWorld } from '../src/index';

describe('Hello World HTTP Function', () => {
  it('should return hello', () => {
    const res = helloWorld.run({} as any);
    expect(res).to.equal('Hello!');
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants