UNPKG

@shipengine/connect-loader

Version:

Internal library for loading ShipEngine Connect apps

129 lines 5.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadApp = void 0; const connect_sdk_1 = require("@shipengine/connect-sdk"); const internal_1 = require("@shipengine/connect-sdk/lib/internal"); const read_carrier_app_definition_1 = require("./definitions/read-carrier-app-definition"); const read_order_app_definition_1 = require("./definitions/read-order-app-definition"); const file_cache_1 = require("./file-cache"); const read_app_manifest_1 = require("./read-app-manifest"); const read_definition_1 = require("./read-definition"); const usesGenericRuntime = (manifest) => { const dependencies = [ ...Object.keys(manifest.devDependencies || {}), ...Object.keys(manifest.dependencies || {}), ]; return dependencies.includes('@shipengine/connect-runtime'); }; const getGenericRuntimeAppType = (manifest) => { const dependencies = [ ...Object.keys(manifest.devDependencies || {}), ...Object.keys(manifest.dependencies || {}), ]; const orderSourceApi = '@shipengine/connect-order-source-api'; const carrierApi = '@shipengine/connect-carrier-api'; const freightApi = '@shipengine/connect-freight-api'; if (dependencies.includes(orderSourceApi)) { return connect_sdk_1.AppType.Order; } else if (dependencies.includes(carrierApi)) { return connect_sdk_1.AppType.Carrier; } else if (dependencies.includes(freightApi)) { return connect_sdk_1.AppType.Freight; } else { throw new Error('Unknown App Type'); } }; const getGenericRuntimeDeploymentType = (type) => { switch (type) { case connect_sdk_1.AppType.Carrier: return internal_1.DeploymentType.CarrierAPI; case connect_sdk_1.AppType.Freight: return internal_1.DeploymentType.FreightAPI; case connect_sdk_1.AppType.Order: return internal_1.DeploymentType.OrderSourceAPI; } }; /** * Loads a ShipEngine Connect App */ async function loadApp(appPath = '.') { try { file_cache_1.fileCache.startedLoading(); // Read the app's manifest (package.json file) const manifest = await read_app_manifest_1.readAppManifest(appPath); // Read the app's exported definition const [definition, definitionPath] = await read_definition_1.readDefinition(appPath, '.', 'ShipEngine Connect app'); if (usesGenericRuntime(manifest)) { const type = getGenericRuntimeAppType(manifest); const deploymentType = getGenericRuntimeDeploymentType(type); const app = definition; return new internal_1.App({ providerId: app.data.Id, id: app.data.Id, manifest, type, deploymentType, validate: app.validate, getSupportedCountries: app.getSupportedCountries, }); } else if (isCarrierApp(definition)) { const pojo = await read_carrier_app_definition_1.readCarrierAppDefinition(definition, definitionPath, manifest); pojo.deploymentType = internal_1.DeploymentType.LegacyConnectCarrier; return new internal_1.CarrierApp(pojo); } else { const pojo = await read_order_app_definition_1.readOrderAppDefinition(definition, definitionPath, manifest); pojo.deploymentType = internal_1.DeploymentType.LegacyConnectOrder; return new internal_1.OrderApp(pojo); } } catch (originalError) { throw internal_1.error(connect_sdk_1.ErrorCode.AppError, 'Error loading the ShipEngine Connect app:', { originalError }); } finally { // Let the cache know that we're done loading the app, // so the cache can be cleared to free-up memory file_cache_1.fileCache.finishedLoading(); } } exports.loadApp = loadApp; /** * Checks to make sure that an app has enough required and distinguishing properties to determine its type. */ function isCarrierApp(definition) { const requiredCarrierProperties = ['deliveryServices']; const optionalCarrierProperties = [ 'manifestLocations', 'manifestShipments', 'pickupServices', 'createShipment', 'cancelShipments', 'rateShipment', 'trackShipment', 'createManifest', 'schedulePickup', 'cancelPickups', ]; for (const property of requiredCarrierProperties) { if (property in definition) { return true; } } for (const property of optionalCarrierProperties) { if (property in definition) { throw new Error("Carrier app is missing required 'deliveryServices` property"); } } const optionalOrderProperties = ['getSalesOrdersByDate', 'shipmentCreated']; for (const property of optionalOrderProperties) { if (property in definition) { return false; } } throw new Error('Your app is missing some required fields. Please refer to the documentation.'); } //# sourceMappingURL=load-app.js.map