Skip to content

Commit

Permalink
refactor!: rename cloudevent to CloudEvent (#379)
Browse files Browse the repository at this point in the history
This commit is a refactor of all our cloudevent identifiers to be more
consistent. Depending on the context we will always prefer: `CloudEvent`,
`cloudEvent`, or `cloud_event`. We will avoid `cloudevent`

Fixes #378
  • Loading branch information
matthewrobertson committed Nov 5, 2021
1 parent 292ade9 commit 11d35b3
Show file tree
Hide file tree
Showing 41 changed files with 78 additions and 78 deletions.
12 changes: 6 additions & 6 deletions docs/generated/api.d.ts
Expand Up @@ -5,25 +5,25 @@ import * as express from 'express';
/**
* Register a function that handles CloudEvents.
* @param functionName - the name of the function
* @param handler - the function to trigger when handling cloudevents
* @param handler - the function to trigger when handling CloudEvents
* @public
*/
export declare const cloudevent: (functionName: string, handler: CloudEventFunction) => void;
export declare const cloudEvent: (functionName: string, handler: CloudEventFunction) => void;

/**
* A cloudevent function handler.
* A CloudEvent function handler.
* @public
*/
export declare interface CloudEventFunction {
(cloudevent: CloudEventsContext): any;
(cloudEvent: CloudEventsContext): any;
}

/**
* A cloudevent function handler with callback.
* A CloudEvent function handler with callback.
* @public
*/
export declare interface CloudEventFunctionWithCallback {
(cloudevent: CloudEventsContext, callback: Function): any;
(cloudEvent: CloudEventsContext, callback: Function): any;
}

/**
Expand Down
20 changes: 10 additions & 10 deletions docs/generated/api.json
Expand Up @@ -171,12 +171,12 @@
"members": [
{
"kind": "Variable",
"canonicalReference": "@google-cloud/functions-framework!cloudevent:var",
"docComment": "/**\n * Register a function that handles CloudEvents.\n *\n * @param functionName - the name of the function\n *\n * @param handler - the function to trigger when handling cloudevents\n *\n * @public\n */\n",
"canonicalReference": "@google-cloud/functions-framework!cloudEvent:var",
"docComment": "/**\n * Register a function that handles CloudEvents.\n *\n * @param functionName - the name of the function\n *\n * @param handler - the function to trigger when handling CloudEvents\n *\n * @public\n */\n",
"excerptTokens": [
{
"kind": "Content",
"text": "cloudevent: "
"text": "cloudEvent: "
},
{
"kind": "Content",
Expand All @@ -193,7 +193,7 @@
}
],
"releaseTag": "Public",
"name": "cloudevent",
"name": "cloudEvent",
"variableTypeTokenRange": {
"startIndex": 1,
"endIndex": 4
Expand All @@ -202,7 +202,7 @@
{
"kind": "Interface",
"canonicalReference": "@google-cloud/functions-framework!CloudEventFunction:interface",
"docComment": "/**\n * A cloudevent function handler.\n *\n * @public\n */\n",
"docComment": "/**\n * A CloudEvent function handler.\n *\n * @public\n */\n",
"excerptTokens": [
{
"kind": "Content",
Expand All @@ -219,7 +219,7 @@
"excerptTokens": [
{
"kind": "Content",
"text": "(cloudevent: "
"text": "(cloudEvent: "
},
{
"kind": "Reference",
Expand Down Expand Up @@ -247,7 +247,7 @@
"overloadIndex": 1,
"parameters": [
{
"parameterName": "cloudevent",
"parameterName": "cloudEvent",
"parameterTypeTokenRange": {
"startIndex": 1,
"endIndex": 2
Expand All @@ -261,7 +261,7 @@
{
"kind": "Interface",
"canonicalReference": "@google-cloud/functions-framework!CloudEventFunctionWithCallback:interface",
"docComment": "/**\n * A cloudevent function handler with callback.\n *\n * @public\n */\n",
"docComment": "/**\n * A CloudEvent function handler with callback.\n *\n * @public\n */\n",
"excerptTokens": [
{
"kind": "Content",
Expand All @@ -278,7 +278,7 @@
"excerptTokens": [
{
"kind": "Content",
"text": "(cloudevent: "
"text": "(cloudEvent: "
},
{
"kind": "Reference",
Expand Down Expand Up @@ -315,7 +315,7 @@
"overloadIndex": 1,
"parameters": [
{
"parameterName": "cloudevent",
"parameterName": "cloudEvent",
"parameterTypeTokenRange": {
"startIndex": 1,
"endIndex": 2
Expand Down
6 changes: 3 additions & 3 deletions docs/generated/api.md
Expand Up @@ -9,18 +9,18 @@
import * as express from 'express';

// @public
export const cloudevent: (functionName: string, handler: CloudEventFunction) => void;
export const cloudEvent: (functionName: string, handler: CloudEventFunction) => void;

// @public
export interface CloudEventFunction {
// (undocumented)
(cloudevent: CloudEventsContext): any;
(cloudEvent: CloudEventsContext): any;
}

// @public
export interface CloudEventFunctionWithCallback {
// (undocumented)
(cloudevent: CloudEventsContext, callback: Function): any;
(cloudEvent: CloudEventsContext, callback: Function): any;
}

// @public
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -21,7 +21,7 @@
"conformance": "./run_conformance_tests.sh",
"check": "gts check",
"clean": "gts clean",
"compile": "tsc -p .",
"compile": "rm -rf ./build && tsc -p .",
"fix": "gts fix",
"predocs": "npm run compile",
"docs": "api-extractor run --local --verbose",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/function_registry.ts
Expand Up @@ -64,10 +64,10 @@ export const http = (functionName: string, handler: HttpFunction): void => {
/**
* Register a function that handles CloudEvents.
* @param functionName - the name of the function
* @param handler - the function to trigger when handling cloudevents
* @param handler - the function to trigger when handling CloudEvents
* @public
*/
export const cloudevent = (
export const cloudEvent = (
functionName: string,
handler: CloudEventFunction
): void => {
Expand Down
26 changes: 13 additions & 13 deletions src/function_wrappers.ts
Expand Up @@ -17,7 +17,7 @@ import * as domain from 'domain';
import {Request, Response, RequestHandler} from 'express';
import {sendCrashResponse} from './logger';
import {sendResponse} from './invoker';
import {isBinaryCloudEvent, getBinaryCloudEventContext} from './cloudevents';
import {isBinaryCloudEvent, getBinaryCloudEventContext} from './cloud_events';
import {
HttpFunction,
EventFunction,
Expand Down Expand Up @@ -56,17 +56,17 @@ const getOnDoneCallback = (res: Response): OnDoneCallback => {
};

/**
* Helper function to parse a cloudevent object from an HTTP request.
* Helper function to parse a CloudEvent object from an HTTP request.
* @param req an Express HTTP request
* @returns a cloudevent parsed from the request
* @returns a CloudEvent parsed from the request
*/
const parseCloudEventRequest = (req: Request): CloudEventsContext => {
let cloudevent = req.body;
let cloudEvent = req.body;
if (isBinaryCloudEvent(req)) {
cloudevent = getBinaryCloudEventContext(req);
cloudevent.data = req.body;
cloudEvent = getBinaryCloudEventContext(req);
cloudEvent.data = req.body;
}
return cloudevent;
return cloudEvent;
};

/**
Expand Down Expand Up @@ -119,7 +119,7 @@ const wrapHttpFunction = (execute: HttpFunction): RequestHandler => {
};

/**
* Wraps an async cloudevent function in an express RequestHandler.
* Wraps an async CloudEvent function in an express RequestHandler.
* @param userFunction User's function.
* @return An Express hander function that invokes the user function.
*/
Expand All @@ -128,9 +128,9 @@ const wrapCloudEventFunction = (
): RequestHandler => {
const httpHandler = (req: Request, res: Response) => {
const callback = getOnDoneCallback(res);
const cloudevent = parseCloudEventRequest(req);
const cloudEvent = parseCloudEventRequest(req);
Promise.resolve()
.then(() => userFunction(cloudevent))
.then(() => userFunction(cloudEvent))
.then(
result => callback(null, result),
err => callback(err, undefined)
Expand All @@ -140,7 +140,7 @@ const wrapCloudEventFunction = (
};

/**
* Wraps callback style cloudevent function in an express RequestHandler.
* Wraps callback style CloudEvent function in an express RequestHandler.
* @param userFunction User's function.
* @return An Express hander function that invokes the user function.
*/
Expand All @@ -149,8 +149,8 @@ const wrapCloudEventFunctionWithCallback = (
): RequestHandler => {
const httpHandler = (req: Request, res: Response) => {
const callback = getOnDoneCallback(res);
const cloudevent = parseCloudEventRequest(req);
return userFunction(cloudevent, callback);
const cloudEvent = parseCloudEventRequest(req);
return userFunction(cloudEvent, callback);
};
return wrapHttpFunction(httpHandler);
};
Expand Down
8 changes: 4 additions & 4 deletions src/functions.ts
Expand Up @@ -55,18 +55,18 @@ export interface EventFunctionWithCallback {
(data: {}, context: Context, callback: Function): any;
}
/**
* A cloudevent function handler.
* A CloudEvent function handler.
* @public
*/
export interface CloudEventFunction {
(cloudevent: CloudEventsContext): any;
(cloudEvent: CloudEventsContext): any;
}
/**
* A cloudevent function handler with callback.
* A CloudEvent function handler with callback.
* @public
*/
export interface CloudEventFunctionWithCallback {
(cloudevent: CloudEventsContext, callback: Function): any;
(cloudEvent: CloudEventsContext, callback: Function): any;
}
/**
* A function handler.
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -20,4 +20,4 @@ export * from './functions';
/**
* @public
*/
export {http, cloudevent} from './function_registry';
export {http, cloudEvent} from './function_registry';
Expand Up @@ -16,8 +16,8 @@ import {
CE_SERVICE,
EventConversionError,
isBinaryCloudEvent,
} from '../cloudevents';
import {CE_TO_BACKGROUND_TYPE} from './cloudevent_to_background_event';
} from '../cloud_events';
import {CE_TO_BACKGROUND_TYPE} from './cloud_event_to_background_event';
import {CloudFunctionsContext, LegacyEvent} from '../functions';

// Maps GCF Event types to the equivalent CloudEventType
Expand Down Expand Up @@ -193,7 +193,7 @@ export const backgroundEventToCloudEventMiddleware = (

if (service === CE_SERVICE.FIREBASE_AUTH) {
if ('metadata' in data) {
// Some metadata are not consistent between cloudevents and background events
// Some metadata are not consistent between CloudEvents and background events
const metadata: object = data.metadata;
data.metadata = {};
// eslint-disable-next-line prefer-const
Expand All @@ -213,15 +213,15 @@ export const backgroundEventToCloudEventMiddleware = (
const domain = req.body['domain'];
if (!domain) {
throw new EventConversionError(
`Unable convert to ${CE_SERVICE.FIREBASE_DB} cloudevent: invalid domain`
`Unable convert to ${CE_SERVICE.FIREBASE_DB} CloudEvent: invalid domain`
);
}
let location = 'us-central1';
if (domain !== 'firebaseio.com') {
const match = domain.match(/^([\w-]+)\.firebasedatabase\.app$/);
if (!match) {
throw new EventConversionError(
`Unable convert to ${CE_SERVICE.FIREBASE_DB} cloudevent: invalid domain`
`Unable convert to ${CE_SERVICE.FIREBASE_DB} CloudEvent: invalid domain`
);
}
location = match[1];
Expand Down
Expand Up @@ -17,7 +17,7 @@ import {
isBinaryCloudEvent,
getBinaryCloudEventContext,
EventConversionError,
} from '../cloudevents';
} from '../cloud_events';

// Maps CloudEvent types to the equivalent GCF Event type
export const CE_TO_BACKGROUND_TYPE: {[k: string]: string} = {
Expand Down Expand Up @@ -132,7 +132,7 @@ const marshalConvertableCloudEvent = (
// FirebaseAuth resource format
resource = name;
if ('metadata' in data) {
// Some metadata are not consistent between cloudevents and legacy events
// Some metadata are not consistent between CloudEvents and legacy events
const metadata: object = data.metadata;
data.metadata = {};
// eslint-disable-next-line prefer-const
Expand Down Expand Up @@ -177,7 +177,7 @@ const marshalConvertableCloudEvent = (
* @param res express response object
* @param next function used to pass control to the next middle middleware function in the stack
*/
export const cloudeventToBackgroundEventMiddleware = (
export const cloudEventToBackgroundEventMiddleware = (
req: Request,
res: Response,
next: NextFunction
Expand Down
2 changes: 1 addition & 1 deletion src/pubsub_middleware.ts
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import {Request, Response, NextFunction} from 'express';
import {isBinaryCloudEvent} from './cloudevents';
import {isBinaryCloudEvent} from './cloud_events';

const PUBSUB_EVENT_TYPE = 'google.pubsub.topic.publish';
const PUBSUB_MESSAGE_TYPE =
Expand Down
6 changes: 3 additions & 3 deletions src/server.ts
Expand Up @@ -20,8 +20,8 @@ import {HandlerFunction, Request, Response} from './functions';
import {SignatureType} from './types';
import {setLatestRes} from './invoker';
import {legacyPubSubEventMiddleware} from './pubsub_middleware';
import {cloudeventToBackgroundEventMiddleware} from './middleware/cloudevent_to_background_event';
import {backgroundEventToCloudEventMiddleware} from './middleware/background_event_to_cloudevent';
import {cloudEventToBackgroundEventMiddleware} from './middleware/cloud_event_to_background_event';
import {backgroundEventToCloudEventMiddleware} from './middleware/background_event_to_cloud_event';
import {wrapUserFunction} from './function_wrappers';

/**
Expand Down Expand Up @@ -108,7 +108,7 @@ export function getServer(
}

if (functionSignatureType === 'event') {
app.use(cloudeventToBackgroundEventMiddleware);
app.use(cloudEventToBackgroundEventMiddleware);
}
if (functionSignatureType === 'cloudevent') {
app.use(backgroundEventToCloudEventMiddleware);
Expand Down
12 changes: 6 additions & 6 deletions test/conformance/function.js
Expand Up @@ -8,19 +8,19 @@ functions.http('writeHttpDeclarative', (req, res) => {
res.end(200);
});

functions.cloudevent('writeCloudEventDeclarative', cloudevent => {
cloudevent.datacontenttype = 'application/json';
writeJson(cloudevent);
functions.cloudEvent('writeCloudEventDeclarative', cloudEvent => {
cloudEvent.datacontenttype = 'application/json';
writeJson(cloudEvent);
});

function writeHttp(req, res) {
writeJson(req.body);
res.end(200);
}

function writeCloudEvent(cloudevent) {
cloudevent.datacontenttype = 'application/json';
writeJson(cloudevent);
function writeCloudEvent(cloudEvent) {
cloudEvent.datacontenttype = 'application/json';
writeJson(cloudEvent);
}

function writeLegacyEvent(data, context) {
Expand Down
4 changes: 2 additions & 2 deletions test/function_registry.ts
Expand Up @@ -25,8 +25,8 @@ describe('function_registry', () => {
assert.deepStrictEqual((userFunction as () => string)(), 'HTTP_PASS');
});

it('can register cloudevent functions', () => {
FunctionRegistry.cloudevent('ceFunction', () => 'CE_PASS');
it('can register CloudEvent functions', () => {
FunctionRegistry.cloudEvent('ceFunction', () => 'CE_PASS');
const {
userFunction,
signatureType,
Expand Down

0 comments on commit 11d35b3

Please sign in to comment.