Skip to content

Commit db0f9dd

Browse files
committed
added helloWorldPubSub cloud function
1 parent c73c488 commit db0f9dd

File tree

6 files changed

+81
-29
lines changed

6 files changed

+81
-29
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"tslib",
88
"downlevel",
99
"typechecking",
10-
"linebreak"
10+
"linebreak",
11+
"Pubsub"
1112
],
1213
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
1314
"cSpell.enabled": true,

google-cloud.d.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
declare namespace GoogleCloudPlatform {
2+
3+
export namespace CloudFunctions {
4+
export interface PubsubEvent {
5+
eventId: string
6+
timestamp: string // ISO 8601
7+
eventType: string
8+
resource: string
9+
data: PubSub.PubsubMessage
10+
}
11+
12+
type Callback = () => void;
13+
}
14+
15+
// From v.0.13.1
16+
// https://www.npmjs.com/package/@google-cloud/pubsub
17+
export namespace PubSub {
18+
/**
19+
* A message data and its attributes. The message payload must not be empty;
20+
* it must contain either a non-empty data field, or at least one attribute.
21+
*
22+
* @property {string} data
23+
* The message payload.
24+
*
25+
* @property {Object.<string, string>} attributes
26+
* Optional attributes for this message.
27+
*
28+
* @property {string} messageId
29+
* ID of this message, assigned by the server when the message is published.
30+
* Guaranteed to be unique within the topic. This value may be read by a
31+
* subscriber that receives a `PubsubMessage` via a `Pull` call or a push
32+
* delivery. It must not be populated by the publisher in a `Publish` call.
33+
*
34+
* @property {Object} publishTime
35+
* The time at which the message was published, populated by the server when
36+
* it receives the `Publish` call. It must not be populated by the
37+
* publisher in a `Publish` call.
38+
*
39+
* This object should have the same structure as [google.protobuf.Timestamp]{@link external:"google.protobuf.Timestamp"}
40+
* Example: "2017-07-17T07:35:53.742Z"
41+
*
42+
* @class
43+
* @see [google.pubsub.v1.PubsubMessage definition in proto format]{@link https://github.com/googleapis/googleapis/blob/master/google/pubsub/v1/pubsub.proto}
44+
*/
45+
export interface PubsubMessage {
46+
data?: string
47+
attributes: { [key: string]: string; }
48+
messageId: string
49+
publishTime: string
50+
}
51+
}
52+
53+
}

index.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
var settings = require("./settings.json");
4-
console.log(settings);
5-
function helloWorld(req, res) {
3+
const settings = require("./settings.json");
4+
console.log("Function settings: " + settings);
5+
function helloWorldHTTP(req, res) {
66
console.log(req);
7-
/*
8-
res
9-
.status(200)
10-
.type('application/json')
11-
.send(settings)
12-
.end()
13-
*/
147
res
158
.status(200)
169
.type("application/json")
1710
.send("{ \"result\": \"Hello World!\"}")
1811
.end();
1912
}
20-
exports.helloWorld = helloWorld;
13+
exports.helloWorldHTTP = helloWorldHTTP;
14+
function helloWorldPubSub(event, callback) {
15+
console.log("Hello World! I have got event: ", event);
16+
callback();
17+
}
18+
exports.helloWorldPubSub = helloWorldPubSub;

index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import * as express from "express"
1+
import {Request, Response} from "express"
2+
3+
import PubsubEvent = GoogleCloudPlatform.CloudFunctions.PubsubEvent
4+
import Callback = GoogleCloudPlatform.CloudFunctions.Callback
25

36
import * as settings from "./settings.json"
47

5-
console.log(settings)
8+
console.log("Function settings: " + settings)
69

7-
export function helloWorld(req: express.Request, res: express.Response) {
10+
export function helloWorldHTTP(req: Request, res: Response) {
811
console.log(req)
912

10-
/*
11-
res
12-
.status(200)
13-
.type('application/json')
14-
.send(settings)
15-
.end()
16-
*/
17-
1813
res
1914
.status(200)
2015
.type("application/json")
2116
.send("{ \"result\": \"Hello World!\"}")
2217
.end()
2318
}
19+
20+
export function helloWorldPubSub(event: PubsubEvent, callback: Callback) {
21+
console.log("Hello World! I have got event: ", event)
22+
callback()
23+
}

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
"description": "Google Cloud Functions TypeScript starter project",
55
"main": "index.js",
66
"scripts": {
7-
"deploy-local": "tsc && functions deploy helloWorld --trigger-http",
8-
"package-zip(windows)": "powershell Compress-Archive -Force -LiteralPath index.js, package.json, settings.json -DestinationPath helloWorld.zip",
9-
"package-zip(linux)": "zip helloWorld.zip index.js package.json settings.json"
7+
"deploy-local": "tsc && functions deploy helloWorldHTTP --trigger-http",
8+
"package-zip(windows)": "tsc && powershell Compress-Archive -Force -LiteralPath index.js, package.json, settings.json -DestinationPath helloWorld.zip",
9+
"package-zip(linux)": "tsc && zip helloWorld.zip index.js package.json settings.json"
1010
},
1111
"author": "Aleksandr Sokolovskii",
1212
"license": "Apache-2.0",
1313
"devDependencies": {
14-
"@types/node": "^6.0.0",
15-
"@types/express": "^4.0.0"
14+
"@types/express": "^4.0.0",
15+
"@types/node": "^6.0.0"
1616
},
1717
"repository": {
1818
"type": "git",

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
/* Basic Options */
4-
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
4+
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
55
"module": "commonjs", /* Specify module code generation: 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
66
// "lib": [], /* Specify library files to be included in the compilation: */
77
"allowJs": true, /* Allow javascript files to be compiled. */

0 commit comments

Comments
 (0)