@mbc-cqrs-serverless/core
Version:
CQRS and event base core
102 lines • 3.89 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var AppSyncService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppSyncService = void 0;
const sha256_js_1 = require("@aws-crypto/sha256-js");
const credential_provider_node_1 = require("@aws-sdk/credential-provider-node");
const signature_v4_1 = require("@aws-sdk/signature-v4");
const common_1 = require("@nestjs/common");
const config_1 = require("@nestjs/config");
const node_fetch_1 = __importDefault(require("node-fetch"));
const query = /* GraphQL */ `
mutation SEND_MESSAGE($message: AWSJSON!) {
sendMessage(message: $message) {
id
table
pk
sk
tenantCode
action
content
}
}
`;
let AppSyncService = AppSyncService_1 = class AppSyncService {
constructor(config) {
this.config = config;
this.logger = new common_1.Logger(AppSyncService_1.name);
this.endpoint = config.get('APPSYNC_ENDPOINT');
this.apiKey = config.get('APPSYNC_API_KEY');
this.region = 'ap-northeast-1';
this.hostname = new URL(this.endpoint).hostname;
this.signer = new signature_v4_1.SignatureV4({
credentials: (0, credential_provider_node_1.defaultProvider)(),
region: this.region,
service: 'appsync',
sha256: sha256_js_1.Sha256,
});
}
async sendMessage(msg) {
const headers = {
'Content-Type': 'application/json',
host: this.hostname,
};
const body = JSON.stringify({
query,
variables: {
message: JSON.stringify(msg),
},
});
const method = 'POST';
let res;
if (this.apiKey) {
headers['x-api-key'] = this.apiKey;
res = await (0, node_fetch_1.default)(this.endpoint, {
method,
headers,
body,
});
}
else {
const request = {
method,
headers,
protocol: 'https:',
hostname: this.hostname,
path: '/graphql',
body,
region: this.region,
service: 'appsync',
};
const signedRequest = await this.signer.sign(request, {
signingDate: new Date(),
});
res = await (0, node_fetch_1.default)(this.endpoint, {
method: signedRequest.method,
headers: signedRequest.headers,
body: signedRequest.body,
});
}
const data = await res.json();
this.logger.debug('appsync send message successfully:: ', data);
return data;
}
};
exports.AppSyncService = AppSyncService;
exports.AppSyncService = AppSyncService = AppSyncService_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [config_1.ConfigService])
], AppSyncService);
//# sourceMappingURL=appsync.service.js.map