|
| 1 | +/* |
| 2 | + * |
| 3 | + * Testing tools for invertase/react-native-firebase use only. |
| 4 | + * |
| 5 | + * Copyright (C) 2018-present Invertase Limited <oss@invertase.io> |
| 6 | + * |
| 7 | + * See License file for more information. |
| 8 | + */ |
| 9 | + |
| 10 | +import * as assert from 'assert'; |
| 11 | +import { FirebaseError } from 'firebase-admin'; |
| 12 | +import * as functions from 'firebase-functions'; |
| 13 | +import SAMPLE_DATA from './sample-data'; |
| 14 | + |
| 15 | +export const testFunctionDefaultRegion = functions.https.onCall(data => { |
| 16 | + console.log(Date.now(), data); |
| 17 | + |
| 18 | + if (typeof data === 'undefined') { |
| 19 | + return 'undefined'; |
| 20 | + } |
| 21 | + |
| 22 | + if (typeof data === 'string') { |
| 23 | + return 'string'; |
| 24 | + } |
| 25 | + |
| 26 | + if (typeof data === 'number') { |
| 27 | + return 'number'; |
| 28 | + } |
| 29 | + |
| 30 | + if (typeof data === 'boolean') { |
| 31 | + return 'boolean'; |
| 32 | + } |
| 33 | + |
| 34 | + if (data === null) { |
| 35 | + return 'null'; |
| 36 | + } |
| 37 | + |
| 38 | + if (Array.isArray(data)) { |
| 39 | + return 'array'; |
| 40 | + } |
| 41 | + |
| 42 | + const { type, asError, inputData } = data; |
| 43 | + if (!Object.hasOwnProperty.call(SAMPLE_DATA, type)) { |
| 44 | + throw new functions.https.HttpsError('invalid-argument', 'Invalid test requested.'); |
| 45 | + } |
| 46 | + |
| 47 | + const outputData = SAMPLE_DATA[type]; |
| 48 | + |
| 49 | + try { |
| 50 | + assert.deepEqual(outputData, inputData); |
| 51 | + } catch (e) { |
| 52 | + console.error(e); |
| 53 | + throw new functions.https.HttpsError( |
| 54 | + 'invalid-argument', |
| 55 | + 'Input and Output types did not match.', |
| 56 | + (e as FirebaseError).message, |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + // all good |
| 61 | + if (asError) { |
| 62 | + throw new functions.https.HttpsError( |
| 63 | + 'cancelled', |
| 64 | + 'Response data was requested to be sent as part of an Error payload, so here we are!', |
| 65 | + outputData, |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + return outputData; |
| 70 | +}); |
0 commit comments