-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(apiconnect): initial implemention of ApiConnectComponent #4851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| package-lock=true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| Copyright (c) IBM Corp. 2020. All Rights Reserved. | ||
| Node module: @loopback/apiconnect | ||
| This project is licensed under the MIT License, full text below. | ||
|
|
||
| -------- | ||
|
|
||
| MIT license | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # @loopback/apiconnect | ||
|
|
||
| This module extends LoopBack with the ability to integrate with | ||
| [IBM API Connect](https://www.ibm.com/cloud/api-connect). It comes with an | ||
| `ApiConnectComponent` that adds an `OASEnhencer` extension to contribute | ||
| `x-ibm-configuration` to the OpenAPI spec generated by LoopBack applications. | ||
|
|
||
| ## Stability: ⚠️Experimental⚠️ | ||
|
|
||
| > Experimental packages provide early access to advanced or experimental | ||
| > functionality to get community feedback. Such modules are published to npm | ||
| > using `0.x.y` versions. Their APIs and functionality may be subject to | ||
| > breaking changes in future releases. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```sh | ||
| npm i @loopback/apiconnect --save | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| The component should be loaded in the constructor of your custom Application | ||
| class. | ||
|
|
||
| Start by importing the component class: | ||
|
|
||
| ```ts | ||
| import {ApiConnectComponent} from '@loopback/apiconnect'; | ||
| ``` | ||
|
|
||
| In the constructor, add the component to your application: | ||
|
|
||
| ```ts | ||
| this.component(ApiConnectComponent); | ||
| ``` | ||
|
|
||
| The component requires a configuration for API Connect extension for OpenAPI | ||
| spec. The `targetUrl` tells API Connect gateway where the REST APIs are served. | ||
|
|
||
| ```ts | ||
| const apiConnectOptions: ApiConnectSpecOptions = { | ||
| targetUrl: 'http://localhost:3000/test-service', | ||
| }; | ||
| app | ||
| .configure(ApiConnectBindings.API_CONNECT_SPEC_ENHANCER) | ||
| .to(apiConnectOptions); | ||
| ``` | ||
|
|
||
| ## Contributions | ||
|
|
||
| - [Guidelines](https://github.com/strongloop/loopback-next/blob/master/docs/CONTRIBUTING.md) | ||
| - [Join the team](https://github.com/strongloop/loopback-next/issues/110) | ||
|
|
||
| ## Tests | ||
|
|
||
| Run `npm test` from the root folder. | ||
|
|
||
| ## Contributors | ||
|
|
||
| See | ||
| [all contributors](https://github.com/strongloop/loopback-next/graphs/contributors). | ||
|
|
||
| ## License | ||
|
|
||
| MIT | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/apiconnect | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| export * from './dist'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/apiconnect | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| module.exports = require('./dist'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/apiconnect | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| // DO NOT EDIT THIS FILE | ||
| // Add any additional (re)exports to src/index.ts instead. | ||
| export * from './src'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| { | ||
| "name": "@loopback/apiconnect", | ||
| "version": "0.0.1", | ||
| "description": "An extension for IBM API Connect", | ||
| "engines": { | ||
| "node": ">=10" | ||
| }, | ||
| "scripts": { | ||
| "acceptance": "lb-mocha \"dist/__tests__/acceptance/**/*.js\"", | ||
| "build:apidocs": "lb-apidocs", | ||
| "build": "lb-tsc", | ||
| "clean": "lb-clean loopback-apiconnect*.tgz dist tsconfig.build.tsbuildinfo package", | ||
| "pretest": "npm run build", | ||
| "test": "lb-mocha \"dist/__tests__/**/*.js\"", | ||
| "unit": "lb-mocha \"dist/__tests__/unit/**/*.js\"", | ||
| "verify": "npm pack && tar xf loopback-apiconnect*.tgz && tree package && npm run clean" | ||
| }, | ||
| "author": "IBM Corp.", | ||
| "copyright.owner": "IBM Corp.", | ||
| "license": "MIT", | ||
| "keywords": [ | ||
| "LoopBack", | ||
| "IBM API Connect" | ||
| ], | ||
| "files": [ | ||
| "README.md", | ||
| "index.js", | ||
| "index.d.ts", | ||
| "dist", | ||
| "src", | ||
| "!*/__tests__" | ||
| ], | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/strongloop/loopback-next.git", | ||
| "directory": "extensions/apiconnect" | ||
| }, | ||
| "dependencies": { | ||
| "@loopback/context": "^3.0.0", | ||
| "@loopback/core": "^2.0.0", | ||
| "@loopback/openapi-v3": "^3.0.0", | ||
| "tslib": "^1.11.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@loopback/rest": "^3.0.0", | ||
| "@loopback/build": "^4.0.0", | ||
| "@loopback/eslint-config": "^6.0.0", | ||
| "@loopback/testlab": "^2.0.0", | ||
| "@types/node": "^10.17.17" | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
extensions/apiconnect/src/__tests__/acceptance/apiconnect.acceptance.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/apiconnect | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {Application} from '@loopback/core'; | ||
| import {RestApplication, RestServer} from '@loopback/rest'; | ||
| import {expect} from '@loopback/testlab'; | ||
| import {ApiConnectBindings, ApiConnectComponent} from '../..'; | ||
| import {ApiConnectSpecOptions} from '../../apiconnect.spec-enhancer'; | ||
|
|
||
| describe('Extension for IBM API Connect - OASEnhancer', () => { | ||
| let app: Application; | ||
| let server: RestServer; | ||
| beforeEach(givenAServer); | ||
|
|
||
| it('adds x-ibm-configuration to apiSpec', async () => { | ||
| const EXPECTED_SPEC = { | ||
| 'x-ibm-configuration': { | ||
| assembly: { | ||
| execute: [ | ||
| { | ||
| invoke: { | ||
| title: 'invoke', | ||
| version: '2.0.0', | ||
| 'target-url': 'http://localhost:3000/test-service', | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| cors: {enabled: true}, | ||
| enforced: true, | ||
| phase: 'realized', | ||
| testable: true, | ||
| gateway: 'datapower-api-gateway', | ||
| }, | ||
| }; | ||
| const spec = await server.getApiSpec(); | ||
| expect(spec).to.containDeep(EXPECTED_SPEC); | ||
| }); | ||
|
|
||
| async function givenAServer() { | ||
| app = new RestApplication(); | ||
| app.component(ApiConnectComponent); | ||
| const apiConnectOptions: ApiConnectSpecOptions = { | ||
| targetUrl: 'http://localhost:3000/test-service', | ||
| }; | ||
| app | ||
| .configure(ApiConnectBindings.API_CONNECT_SPEC_ENHANCER) | ||
| .to(apiConnectOptions); | ||
| server = await app.getServer(RestServer); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/apiconnect | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {Component, createBindingFromClass} from '@loopback/core'; | ||
| import {ApiConnectSpecEnhancer} from './apiconnect.spec-enhancer'; | ||
|
|
||
| export class ApiConnectComponent implements Component { | ||
| bindings = [createBindingFromClass(ApiConnectSpecEnhancer)]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/apiconnect | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {bind, config} from '@loopback/core'; | ||
| import {asSpecEnhancer, OASEnhancer, OpenAPIObject} from '@loopback/openapi-v3'; | ||
|
|
||
| /** | ||
| * Configuration for IBM API Connect extensions to the OpenAPI spec | ||
| */ | ||
| export type ApiConnectSpecOptions = { | ||
| targetUrl: string; | ||
| }; | ||
|
|
||
| /** | ||
| * An OpenAPI spec enhancer to add `x-ibm-configuration` extension required | ||
| * by API Connect | ||
| */ | ||
| @bind(asSpecEnhancer) | ||
| export class ApiConnectSpecEnhancer implements OASEnhancer { | ||
| name = 'IBM API Connect'; | ||
|
|
||
| constructor( | ||
| @config({optional: false}) private options: ApiConnectSpecOptions, | ||
| ) {} | ||
|
|
||
| modifySpec(spec: OpenAPIObject): OpenAPIObject { | ||
| spec['x-ibm-configuration'] = { | ||
| assembly: { | ||
| execute: [ | ||
| { | ||
| invoke: { | ||
| title: 'invoke', | ||
| version: '2.0.0', | ||
| 'target-url': this.options.targetUrl, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| cors: { | ||
| enabled: true, | ||
| }, | ||
| enforced: true, | ||
| phase: 'realized', | ||
| testable: true, | ||
| gateway: 'datapower-api-gateway', | ||
| }; | ||
| return spec; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/apiconnect | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| export * from './apiconnect.component'; | ||
| export * from './apiconnect.spec-enhancer'; | ||
| export * from './keys'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/apiconnect | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {BindingKey} from '@loopback/core'; | ||
| import {ApiConnectSpecEnhancer} from './apiconnect.spec-enhancer'; | ||
|
|
||
| export namespace ApiConnectBindings { | ||
| /** | ||
| * Strongly-typed binding key for ApiConnectSpecEnhancer | ||
| */ | ||
| export const API_CONNECT_SPEC_ENHANCER = BindingKey.create< | ||
| ApiConnectSpecEnhancer | ||
| >('oas-enhancer.ApiConnectSpecEnhancer'); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/tsconfig", | ||
| "extends": "@loopback/build/config/tsconfig.common.json", | ||
| "compilerOptions": { | ||
| "outDir": "dist", | ||
| "rootDir": "src" | ||
| }, | ||
| "include": [ | ||
| "src" | ||
| ] | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.