UNPKG

@golemio/parkings

Version:
102 lines 5.21 kB
"use strict"; 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 __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Smart4CityGraphQLDataSource = void 0; const Smart4CityGraphQLSchemas_1 = require("../../../../schema-definitions/datasources/smart4city/Smart4CityGraphQLSchemas"); const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken"); const integration_engine_1 = require("@golemio/core/dist/integration-engine"); const HTTPFetchProtocolStrategy_1 = require("@golemio/core/dist/integration-engine/datasources/protocol-strategy/HTTPFetchProtocolStrategy"); const golemio_validator_1 = require("@golemio/core/dist/shared/golemio-validator"); const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe"); let Smart4CityGraphQLDataSource = exports.Smart4CityGraphQLDataSource = class Smart4CityGraphQLDataSource { constructor(config) { this.config = config; this.token = null; this.tokenExpireAt = null; this.TOKEN_REFRESH_BUFFER_SECONDS = 60; // Refresh 60 seconds before expiry this.GRANT_TYPE = "client_credentials"; this.QUERY = `query Parking_locations { parking_locations { _places capacity code definition last_changes name total_occ } }`; this.credentialInfo = { tokenUrl: config.getValue("module.parking.Smart4City.graphQL.tokenUrl"), graphQLUrl: config.getValue("module.parking.Smart4City.graphQL.url"), clientId: config.getValue("module.parking.Smart4City.graphQL.clientId"), clientSecret: config.getValue("module.parking.Smart4City.graphQL.clientSecret"), }; } async refreshToken() { // Prepare form data const params = new URLSearchParams(); params.append("grant_type", this.GRANT_TYPE); params.append("client_id", this.credentialInfo.clientId); params.append("client_secret", this.credentialInfo.clientSecret); // Make token request const response = await fetch(this.credentialInfo.tokenUrl, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json", }, body: params.toString(), }); if (!response.ok) { const errorText = await response.text(); throw new Error(`OAuth2 token request failed: ${response.status} ${response.statusText}. Response: ${errorText}`); } const tokenResponse = await response.json(); if (!tokenResponse.access_token) { throw new Error("OAuth2 token response missing access_token"); } // Cache the token with expiration const expiresIn = tokenResponse.expires_in || 3600; // Default 1 hour if not specified const expiresAt = Date.now() + (expiresIn - this.TOKEN_REFRESH_BUFFER_SECONDS) * 1000; this.token = tokenResponse.access_token; this.tokenExpireAt = expiresAt; return tokenResponse.access_token; } async getToken() { const now = Date.now(); if (this.token && this.tokenExpireAt && this.tokenExpireAt > now) { return this.token; } await this.refreshToken(); return this.token; } getDataSource(token) { return new integration_engine_1.DataSource("Smart4CityGraphQLDatasource", new HTTPFetchProtocolStrategy_1.HTTPFetchProtocolStrategy({ headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}`, }, method: "POST", url: this.credentialInfo.graphQLUrl, body: JSON.stringify({ query: this.QUERY }), }), new integration_engine_1.JSONDataTypeStrategy({ resultsPath: "" }), new golemio_validator_1.JSONSchemaValidator("Smart4CityGraphQLDatasourceValidator", Smart4CityGraphQLSchemas_1.smart4CityGraphQLResponse)); } }; exports.Smart4CityGraphQLDataSource = Smart4CityGraphQLDataSource = __decorate([ (0, tsyringe_1.injectable)(), __param(0, (0, tsyringe_1.inject)(CoreToken_1.CoreToken.SimpleConfig)), __metadata("design:paramtypes", [Object]) ], Smart4CityGraphQLDataSource); //# sourceMappingURL=Smart4CityGraphQLDataSource.js.map