Skip to content

Commit

Permalink
feat(apiconnect): initial implemention of ApiConnectComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Mar 11, 2020
1 parent 704bda9 commit cb43549
Show file tree
Hide file tree
Showing 14 changed files with 335 additions and 0 deletions.
1 change: 1 addition & 0 deletions extensions/apiconnect/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=true
25 changes: 25 additions & 0 deletions extensions/apiconnect/LICENSE
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.
66 changes: 66 additions & 0 deletions extensions/apiconnect/README.md
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
6 changes: 6 additions & 0 deletions extensions/apiconnect/index.d.ts
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';
6 changes: 6 additions & 0 deletions extensions/apiconnect/index.js
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');
8 changes: 8 additions & 0 deletions extensions/apiconnect/index.ts
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';
19 changes: 19 additions & 0 deletions extensions/apiconnect/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions extensions/apiconnect/package.json
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"
}
}
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);
}
});
11 changes: 11 additions & 0 deletions extensions/apiconnect/src/apiconnect.component.ts
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)];
}
51 changes: 51 additions & 0 deletions extensions/apiconnect/src/apiconnect.spec-enhancer.ts
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;
}
}
8 changes: 8 additions & 0 deletions extensions/apiconnect/src/index.ts
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';
16 changes: 16 additions & 0 deletions extensions/apiconnect/src/keys.ts
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');
}
11 changes: 11 additions & 0 deletions extensions/apiconnect/tsconfig.build.json
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"
]
}

0 comments on commit cb43549

Please sign in to comment.