@anchan828/nest-cloud-run-queue-tasks-publisher 1.0.3-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +55 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.js +6 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +9 -0
- package/dist/interfaces.d.ts +17 -0
- package/dist/interfaces.js +2 -0
- package/dist/providers.d.ts +3 -0
- package/dist/providers.js +8 -0
- package/dist/publish.module.d.ts +6 -0
- package/dist/publish.module.js +53 -0
- package/dist/publish.service.d.ts +8 -0
- package/dist/publish.service.js +64 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 anchan828
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# @anchan828/nest-cloud-run-queue-tasks-publisher
|
|
2
|
+
|
|
3
|
+
## Install
|
|
4
|
+
|
|
5
|
+
```shell
|
|
6
|
+
npm i @anchan828/nest-cloud-run-queue-tasks-publisher
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
NOTE: You may want to do tutorial for using Tasks with Cloud Run before using them.
|
|
12
|
+
https://cloud.google.com/run/docs/triggering/using-tasks
|
|
13
|
+
|
|
14
|
+
### Import publisher module
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
@Module({
|
|
18
|
+
imports: [
|
|
19
|
+
TasksPublisherModule.register({
|
|
20
|
+
clientConfig: {
|
|
21
|
+
// If necessary
|
|
22
|
+
projectId: "projectId",
|
|
23
|
+
keyFilename: "path/to/file.json",
|
|
24
|
+
},
|
|
25
|
+
publishConfig: {
|
|
26
|
+
httpRequest: {
|
|
27
|
+
// By setting this as the default URL here, you can save yourself the trouble of setting the URL each time.
|
|
28
|
+
url: "https://example-this-is-demo-uc.a.run.app",
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
queue: "projects/test/locations/location/queues/nest-cloud-run-queue-demo",
|
|
32
|
+
}),
|
|
33
|
+
],
|
|
34
|
+
})
|
|
35
|
+
export class PublisherAppModule {}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Publish message to queue
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
export class Service {
|
|
42
|
+
constructor(private readonly tasksService: TasksPublisherService) {}
|
|
43
|
+
|
|
44
|
+
public async sendMessage(): Promise<void> {
|
|
45
|
+
await this.tasksService.publish({
|
|
46
|
+
// Required. this property is used by @anchan828/nest-cloud-run-queue-worker
|
|
47
|
+
name: "Worker name",
|
|
48
|
+
// string or object. ex, { text: "text" }
|
|
49
|
+
data: "text",
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
See more information: [https://github.com/anchan828/nest-cloud-run-queue#readme](https://github.com/anchan828/nest-cloud-run-queue#readme)
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ERROR_QUEUE_NOT_FOUND = exports.TASKS_CLIENT = exports.TASKS_PUBLISHER_MODULE_OPTIONS = void 0;
|
|
4
|
+
exports.TASKS_PUBLISHER_MODULE_OPTIONS = "TASKS_PUBLISHER_MODULE_OPTIONS";
|
|
5
|
+
exports.TASKS_CLIENT = "TASKS_CLIENT";
|
|
6
|
+
exports.ERROR_QUEUE_NOT_FOUND = "Queue not found. Please set queue name.";
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { TasksPublisherModuleAsyncOptions, TasksPublisherModuleOptions, TasksPublisherModuleOptionsFactory, PublishConfig, PublishData, PublishOptions, } from "./interfaces";
|
|
2
|
+
export { TASKS_CLIENT } from "./constants";
|
|
3
|
+
export { TasksPublisherModule } from "./publish.module";
|
|
4
|
+
export { TasksPublisherService } from "./publish.service";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TasksPublisherService = exports.TasksPublisherModule = exports.TASKS_CLIENT = void 0;
|
|
4
|
+
var constants_1 = require("./constants");
|
|
5
|
+
Object.defineProperty(exports, "TASKS_CLIENT", { enumerable: true, get: function () { return constants_1.TASKS_CLIENT; } });
|
|
6
|
+
var publish_module_1 = require("./publish.module");
|
|
7
|
+
Object.defineProperty(exports, "TasksPublisherModule", { enumerable: true, get: function () { return publish_module_1.TasksPublisherModule; } });
|
|
8
|
+
var publish_service_1 = require("./publish.service");
|
|
9
|
+
Object.defineProperty(exports, "TasksPublisherService", { enumerable: true, get: function () { return publish_service_1.TasksPublisherService; } });
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Message, ModuleAsyncOptions, ModuleOptions, ModuleOptionsFactory, PublishExtraConfig } from "@anchan828/nest-cloud-run-queue-common";
|
|
2
|
+
import type { google } from "@google-cloud/tasks/build/protos/protos";
|
|
3
|
+
import { ClientOptions, CallOptions } from "google-gax";
|
|
4
|
+
export interface TasksPublisherModuleOptions extends ModuleOptions {
|
|
5
|
+
queue?: string;
|
|
6
|
+
clientConfig?: ClientOptions;
|
|
7
|
+
publishConfig?: PublishConfig;
|
|
8
|
+
extraConfig?: PublishExtraConfig;
|
|
9
|
+
}
|
|
10
|
+
export declare type TasksPublisherModuleAsyncOptions = ModuleAsyncOptions<TasksPublisherModuleOptions>;
|
|
11
|
+
export declare type TasksPublisherModuleOptionsFactory = ModuleOptionsFactory<TasksPublisherModuleOptions>;
|
|
12
|
+
export declare type PublishConfig = Omit<google.cloud.tasks.v2.ITask, "name">;
|
|
13
|
+
export declare type PublishOptions = {
|
|
14
|
+
queue?: string;
|
|
15
|
+
gaxOpts?: CallOptions;
|
|
16
|
+
} & PublishConfig;
|
|
17
|
+
export declare type PublishData<T> = Message<T>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createClient = void 0;
|
|
4
|
+
const tasks_1 = require("@google-cloud/tasks");
|
|
5
|
+
function createClient(options) {
|
|
6
|
+
return new tasks_1.CloudTasksClient(options.clientConfig);
|
|
7
|
+
}
|
|
8
|
+
exports.createClient = createClient;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DynamicModule } from "@nestjs/common";
|
|
2
|
+
import { TasksPublisherModuleAsyncOptions, TasksPublisherModuleOptions } from "./interfaces";
|
|
3
|
+
export declare class TasksPublisherModule {
|
|
4
|
+
static register(options?: TasksPublisherModuleOptions): DynamicModule;
|
|
5
|
+
static registerAsync(options: TasksPublisherModuleAsyncOptions): DynamicModule;
|
|
6
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var TasksPublisherModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.TasksPublisherModule = void 0;
|
|
11
|
+
const nest_cloud_run_queue_common_1 = require("@anchan828/nest-cloud-run-queue-common");
|
|
12
|
+
const common_1 = require("@nestjs/common");
|
|
13
|
+
const constants_1 = require("./constants");
|
|
14
|
+
const providers_1 = require("./providers");
|
|
15
|
+
const publish_service_1 = require("./publish.service");
|
|
16
|
+
let TasksPublisherModule = TasksPublisherModule_1 = class TasksPublisherModule {
|
|
17
|
+
static register(options = {}) {
|
|
18
|
+
const providers = [
|
|
19
|
+
(0, nest_cloud_run_queue_common_1.createOptionProvider)(constants_1.TASKS_PUBLISHER_MODULE_OPTIONS, options),
|
|
20
|
+
publish_service_1.TasksPublisherService,
|
|
21
|
+
{ provide: constants_1.TASKS_CLIENT, useValue: (0, providers_1.createClient)(options) },
|
|
22
|
+
];
|
|
23
|
+
return {
|
|
24
|
+
exports: providers,
|
|
25
|
+
global: true,
|
|
26
|
+
module: TasksPublisherModule_1,
|
|
27
|
+
providers,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
static registerAsync(options) {
|
|
31
|
+
const asyncProviders = [
|
|
32
|
+
...(0, nest_cloud_run_queue_common_1.createAsyncProviders)(constants_1.TASKS_PUBLISHER_MODULE_OPTIONS, options),
|
|
33
|
+
publish_service_1.TasksPublisherService,
|
|
34
|
+
{
|
|
35
|
+
inject: [constants_1.TASKS_PUBLISHER_MODULE_OPTIONS],
|
|
36
|
+
provide: constants_1.TASKS_CLIENT,
|
|
37
|
+
useFactory: (options) => (0, providers_1.createClient)(options),
|
|
38
|
+
},
|
|
39
|
+
];
|
|
40
|
+
const providers = [...asyncProviders];
|
|
41
|
+
return {
|
|
42
|
+
exports: providers,
|
|
43
|
+
global: true,
|
|
44
|
+
imports: [...(options.imports || [])],
|
|
45
|
+
module: TasksPublisherModule_1,
|
|
46
|
+
providers,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
TasksPublisherModule = TasksPublisherModule_1 = __decorate([
|
|
51
|
+
(0, common_1.Module)({})
|
|
52
|
+
], TasksPublisherModule);
|
|
53
|
+
exports.TasksPublisherModule = TasksPublisherModule;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CloudTasksClient } from "@google-cloud/tasks";
|
|
2
|
+
import { TasksPublisherModuleOptions, PublishData, PublishOptions } from "./interfaces";
|
|
3
|
+
export declare class TasksPublisherService {
|
|
4
|
+
private readonly options;
|
|
5
|
+
private readonly client;
|
|
6
|
+
constructor(options: TasksPublisherModuleOptions, client: CloudTasksClient);
|
|
7
|
+
publish<T>(message: PublishData<T>, options?: PublishOptions): Promise<string>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
15
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
16
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
17
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
18
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
19
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
20
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.TasksPublisherService = void 0;
|
|
25
|
+
const tasks_1 = require("@google-cloud/tasks");
|
|
26
|
+
const common_1 = require("@nestjs/common");
|
|
27
|
+
const constants_1 = require("./constants");
|
|
28
|
+
let TasksPublisherService = class TasksPublisherService {
|
|
29
|
+
constructor(options, client) {
|
|
30
|
+
this.options = options;
|
|
31
|
+
this.client = client;
|
|
32
|
+
}
|
|
33
|
+
publish(message, options) {
|
|
34
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
const taskMessage = ((_a = this.options.extraConfig) === null || _a === void 0 ? void 0 : _a.prePublish)
|
|
37
|
+
? yield ((_b = this.options.extraConfig) === null || _b === void 0 ? void 0 : _b.prePublish(message))
|
|
38
|
+
: message;
|
|
39
|
+
const [task] = yield this.client.createTask({
|
|
40
|
+
parent: (options === null || options === void 0 ? void 0 : options.queue) || this.options.queue,
|
|
41
|
+
task: Object.assign(Object.assign(Object.assign({}, this.options.publishConfig), options), { httpRequest: {
|
|
42
|
+
body: Buffer.from(JSON.stringify({ message: { data: taskMessage } })).toString("base64"),
|
|
43
|
+
headers: Object.assign(Object.assign({ "content-type": "application/json" }, (_c = options === null || options === void 0 ? void 0 : options.httpRequest) === null || _c === void 0 ? void 0 : _c.headers), (_e = (_d = this.options.publishConfig) === null || _d === void 0 ? void 0 : _d.httpRequest) === null || _e === void 0 ? void 0 : _e.headers),
|
|
44
|
+
httpMethod: ((_f = options === null || options === void 0 ? void 0 : options.httpRequest) === null || _f === void 0 ? void 0 : _f.httpMethod) || ((_h = (_g = this.options.publishConfig) === null || _g === void 0 ? void 0 : _g.httpRequest) === null || _h === void 0 ? void 0 : _h.httpMethod) || "POST",
|
|
45
|
+
oauthToken: ((_j = options === null || options === void 0 ? void 0 : options.httpRequest) === null || _j === void 0 ? void 0 : _j.oauthToken) || ((_l = (_k = this.options.publishConfig) === null || _k === void 0 ? void 0 : _k.httpRequest) === null || _l === void 0 ? void 0 : _l.oauthToken),
|
|
46
|
+
oidcToken: ((_m = options === null || options === void 0 ? void 0 : options.httpRequest) === null || _m === void 0 ? void 0 : _m.oidcToken) || ((_p = (_o = this.options.publishConfig) === null || _o === void 0 ? void 0 : _o.httpRequest) === null || _p === void 0 ? void 0 : _p.oidcToken),
|
|
47
|
+
url: ((_q = options === null || options === void 0 ? void 0 : options.httpRequest) === null || _q === void 0 ? void 0 : _q.url) || ((_s = (_r = this.options.publishConfig) === null || _r === void 0 ? void 0 : _r.httpRequest) === null || _s === void 0 ? void 0 : _s.url),
|
|
48
|
+
} }),
|
|
49
|
+
});
|
|
50
|
+
const taskName = (task === null || task === void 0 ? void 0 : task.name) || "";
|
|
51
|
+
if ((_t = this.options.extraConfig) === null || _t === void 0 ? void 0 : _t.postPublish) {
|
|
52
|
+
yield ((_u = this.options.extraConfig) === null || _u === void 0 ? void 0 : _u.postPublish(taskMessage, taskName));
|
|
53
|
+
}
|
|
54
|
+
return taskName;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
TasksPublisherService = __decorate([
|
|
59
|
+
(0, common_1.Injectable)(),
|
|
60
|
+
__param(0, (0, common_1.Inject)(constants_1.TASKS_PUBLISHER_MODULE_OPTIONS)),
|
|
61
|
+
__param(1, (0, common_1.Inject)(constants_1.TASKS_CLIENT)),
|
|
62
|
+
__metadata("design:paramtypes", [Object, tasks_1.CloudTasksClient])
|
|
63
|
+
], TasksPublisherService);
|
|
64
|
+
exports.TasksPublisherService = TasksPublisherService;
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@anchan828/nest-cloud-run-queue-tasks-publisher",
|
|
3
|
+
"version": "1.0.3-next.0",
|
|
4
|
+
"description": "> TODO: description",
|
|
5
|
+
"homepage": "https://github.com/anchan828/nest-cloud-run-queue/tree/master/packages/pubsub-publish#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/anchan828/nest-cloud-run-queue/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/anchan828/nest-cloud-run-queue.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "anchan828 <anchan828@gmail.com>",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"directories": {
|
|
18
|
+
"dist": "dist"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc -p tsconfig.build.json",
|
|
25
|
+
"build:watch": "tsc --watch",
|
|
26
|
+
"copy:license": "cp ../../LICENSE ./",
|
|
27
|
+
"lint": "eslint --ignore-path ../../.eslintignore '**/*.ts'",
|
|
28
|
+
"lint:fix": "npm run lint -- --fix",
|
|
29
|
+
"prepublishOnly": "rm -rf dist && npm run build && rm -f dist/*.tsbuildinfo && npm run copy:license",
|
|
30
|
+
"test": "jest --coverage --logHeapUsage --runInBand",
|
|
31
|
+
"test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand --logHeapUsage",
|
|
32
|
+
"test:watch": "jest --watch",
|
|
33
|
+
"watch": "tsc -w"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@anchan828/nest-cloud-run-queue-common": "^1.0.3-next.0",
|
|
37
|
+
"@google-cloud/tasks": "2.5.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@nestjs/common": "8.4.4",
|
|
41
|
+
"rxjs": "7.5.5"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@nestjs/common": "^8.0.0"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"gitHead": "470341e04eead0d62396245ff1a3fe0c7ad4143e"
|
|
50
|
+
}
|