Skip to content

Commit

Permalink
feat: add integration for service-oriented backends
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Apr 20, 2018
1 parent 1ed79c9 commit 418fa32
Show file tree
Hide file tree
Showing 15 changed files with 400 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/service/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
25 changes: 25 additions & 0 deletions packages/service/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) IBM Corp. 2018. All Rights Reserved.
Node module: @loopback/service
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.
39 changes: 39 additions & 0 deletions packages/service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# @loopback/service

This module provides a common set of interfaces for interacting with service
oriented backends such as REST APIs, SOAP Web Services, and gRPC microservices.

## Overview

## Installation

```
$ npm install --save @loopback/service
```

## Basic use

## Concepts

### Service

## Use Service

### Define legacy data sources

## Contributions

- [Guidelines](https://github.com/strongloop/loopback-next/wiki/Contributing##guidelines)
- [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
8 changes: 8 additions & 0 deletions packages/service/docs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"content": [
"./index.ts",
"./src/index.ts",
"./src/service-proxy.ts"
],
"codeSectionDepth": 4
}
6 changes: 6 additions & 0 deletions packages/service/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/service
// 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 packages/service/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/service
// 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 packages/service/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: @loopback/service
// 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';
45 changes: 45 additions & 0 deletions packages/service/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "@loopback/service",
"version": "0.2.0",
"description": "Service integration for LoopBack 4",
"engines": {
"node": ">=8"
},
"main": "index",
"scripts": {
"acceptance": "lb-mocha \"DIST/test/acceptance/**/*.js\"",
"build": "lb-tsc es2017",
"build:apidocs": "lb-apidocs",
"clean": "lb-clean loopback-service*.tgz dist package api-docs",
"prepublishOnly": "npm run build && npm run build:apidocs",
"pretest": "npm run build",
"test": "lb-mocha \"DIST/test/unit/**/*.js\" \"DIST/test/acceptance/**/*.js\"",
"unit": "lb-mocha \"DIST/test/unit/**/*.js\"",
"verify": "npm pack && tar xf loopback-service*.tgz && tree package && npm run clean"
},
"author": "IBM",
"copyright.owner": "IBM Corp.",
"license": "MIT",
"devDependencies": {
"@loopback/build": "^0.2.0",
"@loopback/testlab": "^0.2.0",
"loopback-connector-rest": "^3.1.0"
},
"dependencies": {
"@loopback/context": "^0.2.0",
"@loopback/core": "^0.2.0",
"loopback-datasource-juggler": "^3.15.2"
},
"files": [
"README.md",
"index.js",
"index.d.ts",
"dist/src",
"api-docs",
"src"
],
"repository": {
"type": "git",
"url": "https://github.com/strongloop/loopback-next.git"
}
}
8 changes: 8 additions & 0 deletions packages/service/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: @loopback/service
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './legacy-juggler-bridge';
export * from './loopback-datasource-juggler';
export * from './service-proxy';
14 changes: 14 additions & 0 deletions packages/service/src/legacy-juggler-bridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: @loopback/service
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const jugglerModule = require('loopback-datasource-juggler');

import {juggler} from './loopback-datasource-juggler';

export const DataSourceConstructor = jugglerModule.DataSource as typeof juggler.DataSource;

export function getService<T>(ds: juggler.DataSource): T {
return ds.DataAccessObject as T;
}
20 changes: 20 additions & 0 deletions packages/service/src/loopback-datasource-juggler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/service
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

// tslint:disable:no-any

export declare namespace juggler {
/**
* DataSource instance properties/operations
*/
export class DataSource {
name: string;
settings: {[name: string]: any};
DataAccessObject: {[name: string]: any};

constructor(name?: string, settings?: {[name: string]: any});
constructor(settings?: {[name: string]: any});
}
}
77 changes: 77 additions & 0 deletions packages/service/src/service-proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/service
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

// tslint:disable:no-any

/**
* Invocable represents a weakly-typed object that has a generic `invoke` method
* to handle method invocations
*/
export interface Invocable {
/**
* Properties or methods
*/
[property: string]: any;
/**
* Invoke an operation with an array of arguments
* @param operationName Name of the operation
* @param args An array of arguments
*/
invoke(operationName: string, ...args: any[]): any;
}

/**
* An invoker is responsible for implementing method invocations on the given
* target
*/
export interface Invoker {
invoke(target: any, operationName: string, ...args: any[]): any;
}

function isInvocable(obj: any): obj is Invocable {
return obj != null && typeof obj.invoke === 'function';
}

class InvocableProxyHandler<T extends {[property: string]: any}>
implements ProxyHandler<T> {
get(target: T, propKey: PropertyKey, receiver: any) {
if (propKey in target) {
return target[propKey];
}
if (isInvocable(target)) {
return function(...args: any[]) {
return target.invoke(propKey.toString(), ...args);
};
} else {
return undefined;
}
}
}

class InvokerProxyHandler<T extends object> implements ProxyHandler<T> {
constructor(private invoker: Invoker) {}
get(target: T, propKey: PropertyKey, receiver: any) {
return (...args: any[]) =>
this.invoker.invoke(target, propKey.toString(), ...args);
}
}

const DEFAULT_HANDLER = new InvocableProxyHandler<any>();

/**
* Create a proxy for the given object
* @param obj The source object
* @param invoker The optional invoker that handles invocation of methods that
* do not exist on the source object. If not provided, the proxy will try to
* use the `invoke` method if it is implemented by the source object. If
* neither an invoker nor the invoke method is present, the proxy only allows
* existing properties and methods on the source object.
*/
export function proxify<T extends object>(obj: T, invoker?: Invoker): T {
if (invoker) {
return new Proxy<T>(obj, new InvokerProxyHandler(invoker));
}
return new Proxy<T>(obj, DEFAULT_HANDLER);
}
68 changes: 68 additions & 0 deletions packages/service/test/unit/service-proxy/service-proxy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: @loopback/service
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {expect} from '@loopback/testlab';

import {proxify, Invocable, Invoker} from '../../..';

// tslint:disable:no-any
describe('proxify', () => {
class MyService implements Invocable {
[property: string]: any;
name = 'MyService';

invoke(operationName: string, ...args: any[]) {
return operationName + ': ' + args;
}

getName() {
return this.name;
}
}

class YourService {
[property: string]: any;

getName() {
return 'YourService';
}
}

class MyInvoker implements Invoker {
invoke(target: any, operationName: string, ...args: any[]) {
return operationName + ': ' + args;
}
}

it('creates a proxy', () => {
const proxy = proxify(new MyService());
let result = proxy.x(1);
expect(result).to.eql('x: 1');
result = proxy.y('a', 1);
expect(result).to.eql('y: a,1');
});

it('honors existing methods', () => {
const proxy = proxify(new MyService());
let result = proxy.getName();
expect(result).to.eql('MyService');
});

it('creates a proxy from an invoker', () => {
const proxy = proxify({} as {[property: string]: any}, new MyInvoker());
let result = proxy.x(1);
expect(result).to.eql('x: 1');
result = proxy.y('a', 1);
expect(result).to.eql('y: a,1');
});

it('creates a proxy from a regular object', () => {
const proxy = proxify(new YourService());
let result = proxy.getName();
expect(result).to.eql('YourService');

expect(proxy.x).to.be.undefined();
});
});
Loading

0 comments on commit 418fa32

Please sign in to comment.