in src/main.ts [273:317]
name: _makeResourceName(eventResource, params),
},
eventType,
timestamp: new Date().toISOString(),
params,
};
return defaultContext;
}
function _extractDatabaseParams(
triggerResource: string,
data: database.DataSnapshot
): EventContext['params'] {
const path = data.ref.toString().replace(data.ref.root.toString(), '');
return _extractParams(triggerResource, path);
}
function _extractFirestoreDocumentParams(
triggerResource: string,
data: firestore.DocumentSnapshot
): EventContext['params'] {
// Resource format: databases/(default)/documents/<path>
return _extractParams(
triggerResource.replace(/^databases\/[^\/]+\/documents\//, ''),
data.ref.path
);
}
/**
* Extracts the `{wildcard}` values from `dataPath`.
* E.g. A wildcard path of `users/{userId}` with `users/FOO` would result in `{ userId: 'FOO' }`.
* @internal
*/
export function _extractParams(
wildcardTriggerPath: string,
dataPath: string
): EventContext['params'] {
// Trim start and end / and split into path components
const wildcardPaths = wildcardTriggerPath
.replace(/^\/?(.*?)\/?$/, '$1')
.split('/');
const dataPaths = dataPath.replace(/^\/?(.*?)\/?$/, '$1').split('/');
const params = {};
if (wildcardPaths.length === dataPaths.length) {
for (let idx = 0; idx < wildcardPaths.length; idx++) {