-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add integration for service-oriented backends
- Loading branch information
1 parent
1ed79c9
commit 5fb6604
Showing
17 changed files
with
565 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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=false |
This file contains 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. 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. |
This file contains 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,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 |
This file contains 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 @@ | ||
{ | ||
"content": [ | ||
"./index.ts", | ||
"./src/index.ts", | ||
"./src/service-proxy.ts" | ||
], | ||
"codeSectionDepth": 4 | ||
} |
This file contains 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. 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'; |
This file contains 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. 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'); |
This file contains 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. 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'; |
This file contains 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,44 @@ | ||
{ | ||
"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", | ||
"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.8.0", | ||
"@loopback/core": "^0.6.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" | ||
} | ||
} |
This file contains 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,75 @@ | ||
import { | ||
DecoratorFactory, | ||
ParameterDecoratorFactory, | ||
MetadataAccessor, | ||
inject, | ||
Context, | ||
Injection, | ||
InjectionMetadata, | ||
} from '@loopback/context'; | ||
import {getService, juggler} from '..'; | ||
|
||
// 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 | ||
|
||
/** | ||
* Type definition for decorators returned by `@serviceProxy` decorator factory | ||
*/ | ||
export type ServiceProxyDecorator = PropertyDecorator | ParameterDecorator; | ||
|
||
export const SERVICE_PROXY_KEY = MetadataAccessor.create< | ||
string, | ||
ServiceProxyDecorator | ||
>('service.proxy'); | ||
|
||
/** | ||
* Metadata for a service proxy | ||
*/ | ||
export class ServiceProxyMetadata implements InjectionMetadata { | ||
decorator = '@serviceProxy'; | ||
dataSourceName?: string; | ||
dataSource?: juggler.DataSource; | ||
|
||
constructor(dataSource: string | juggler.DataSource) { | ||
if (typeof dataSource === 'string') { | ||
this.dataSourceName = dataSource; | ||
} else { | ||
this.dataSource = dataSource; | ||
} | ||
} | ||
} | ||
|
||
export function serviceProxy(dataSource: string | juggler.DataSource) { | ||
return function( | ||
target: Object, | ||
key: symbol | string, | ||
parameterIndex?: number, | ||
) { | ||
if (key || typeof parameterIndex === 'number') { | ||
const meta = new ServiceProxyMetadata(dataSource); | ||
inject('', meta, resolve)(target, key, parameterIndex); | ||
} else { | ||
throw new Error( | ||
'@serviceProxy can only be applied to properties or method parameters', | ||
); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Resolve the @repository injection | ||
* @param ctx Context | ||
* @param injection Injection metadata | ||
*/ | ||
async function resolve(ctx: Context, injection: Injection) { | ||
const meta = injection.metadata as ServiceProxyMetadata; | ||
if (meta.dataSource) return getService(meta.dataSource); | ||
if (meta.dataSourceName) { | ||
const ds = await ctx.get<juggler.DataSource>( | ||
'datasources.' + meta.dataSourceName, | ||
); | ||
return getService(ds); | ||
} | ||
} |
This file contains 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,9 @@ | ||
// 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'; | ||
export * from './decorators/service.decorator'; |
This file contains 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,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; | ||
} |
This file contains 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,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}); | ||
} | ||
} |
This file contains 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,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); | ||
} |
Oops, something went wrong.