Skip to content

Commit

Permalink
init project
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Dec 15, 2016
0 parents commit 2a77e52
Show file tree
Hide file tree
Showing 13 changed files with 691 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.debug.js
*.min.js
node_modules/*
57 changes: 57 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"rules": {
"indent": [
2,
2
],
"quotes": [
2,
"single"
],
"linebreak-style": [
2,
"unix"
],
"semi": [2, "always"],
"strict": [2, "global"],
"curly": 2,
"eqeqeq": 2,
"no-eval": 2,
"guard-for-in": 2,
"no-caller": 2,
"no-else-return": 2,
"no-eq-null": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-floating-decimal": 2,
"no-implied-eval": 2,
"no-labels": 2,
"no-with": 2,
"no-loop-func": 1,
"no-native-reassign": 2,
"no-redeclare": [2, {"builtinGlobals": true}],
"no-delete-var": 2,
"no-shadow-restricted-names": 2,
"no-undef-init": 2,
"no-use-before-define": 2,
"no-unused-vars": 2,
"no-undef": 2,
"callback-return": [2, ["callback", "cb", "next"]],
"global-require": 0,
"no-console": 0,
"no-constant-condition": 0,
"generator-star-spacing": ["error", "after"]
},
"env": {
"es6": true,
"node": true,
"browser": true
},
"globals": {
"describe": true,
"it": true,
"before": true,
"after": true
},
"extends": "eslint:recommended"
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
out
coverage
test/config.js
53 changes: 53 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# $@ = target file
# $< = first dependency
# $^ = all dependencies

TESTS = test/*.test.js
REPORTER = spec
TIMEOUT = 20000
ISTANBUL = ./node_modules/.bin/istanbul
MOCHA = ./node_modules/mocha/bin/_mocha
COVERALLS = ./node_modules/coveralls/bin/coveralls.js
DOXMATE = ./node_modules/.bin/doxmate
PATH := ./node_modules/.bin:$(PATH)

lint:
@eslint --fix lib test

doc:
@$(DOXMATE) build -o out

test:
@NODE_ENV=test ./node_modules/.bin/mocha \
--reporter $(REPORTER) \
--require co-mocha \
--timeout $(TIMEOUT) \
$(MOCHA_OPTS) \
$(TESTS)

test-debug:
@NODE_ENV=test ./node_modules/.bin/mocha -d \
--reporter $(REPORTER) \
--require co-mocha \
--timeout $(TIMEOUT) \
$(MOCHA_OPTS) \
$(TESTS)

test-cov:
@NODE_ENV=test node \
node_modules/.bin/istanbul cover --report html \
./node_modules/.bin/_mocha -- \
--reporter $(REPORTER) \
--require co-mocha \
--timeout $(TIMEOUT) \
$(MOCHA_OPTS) \
$(TESTS)

test-coveralls:
@$(ISTANBUL) cover --report lcovonly $(MOCHA) -- -t $(TIMEOUT) -R spec $(TESTS)
@echo TRAVIS_JOB_ID $(TRAVIS_JOB_ID)
@cat ./coverage/lcov.info | $(COVERALLS) && rm -rf ./coverage

test-all: test test-coveralls

.PHONY: test
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
Aliyun API Gateway SDK for Node.js
==================================

API 网关(API Gateway),提供高性能、高可用的 API 托管服务,帮助用户对外开放其部署在 ECS、容器服务等阿里云产品上的应用,提供完整的 API 发布、管理、维护生命周期管理。用户只需简单操作,即可快速、低成本、低风险的开放数据或服务。

## Installation

You can install it as dependency with npm/cnpm.

```sh
$ # save into package.json dependencies with -S
$ npm install aliyun-api-gateway -S
$ # you can use cnpm for fast install
$ cnpm install aliyun-api-gateway -S
```

## Usage

The SDK contains Simple client(authrozied by appcode) and Normal client(authrozied by appid & appsecret).

### Simple client

```js
// require it
var SimpleClient = require('aliyun-api-gateway').SimpleClient;

// create client instance with appcode
var client = new SimpleClient('appcode');

// send GET request
it('should ok get(url)', function* () {
var url = 'https://dm-81.data.aliyun.com/rest/160601/ip/getIpInfo.json?ip=210.75.225.254';
var result = yield client.get(url);
});

it('should ok with post(url)', function* () {
var url = 'https://dm-72.data.aliyun.com/rest/160601/int_image/matching.json';
var result = yield client.post(url, {
data: {
'image': {
'dataType': 10,
'dataValue': 'base64 content'
}
}
});
});
```

### Client (recommend)

```js
// require it
var Client = require('aliyun-api-gateway').Client;

// create client instance with appkey and appsecret
var client = new Client('appKey', 'appSecret');

// send GET request
it('should ok get(url)', function* () {
var url = 'https://dm-81.data.aliyun.com/rest/160601/ip/getIpInfo.json?ip=210.75.225.254';
var result = yield client.get(url);
});

it('should ok with post(url)', function* () {
var url = 'https://dm-72.data.aliyun.com/rest/160601/int_image/matching.json';
var result = yield client.post(url, {
data: {
'image': {
'dataType': 10,
'dataValue': 'base64 content'
}
}
});
});
```

## Question?

Please submit an issue.

## License

The MIT License
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.Client = require('./lib/client');

exports.SimpleClient = require('./lib/simple-client');
31 changes: 31 additions & 0 deletions lib/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const parse = require('url').parse;
const querystring = require('querystring');

/**
* API Gateway Client
*/
class Base {
constructor() {
}

* get(url, opts) {
opts || (opts = {});
var parsed = parse(url, true);
if (opts && opts.data) {
// append data into querystring
Object.assign(parsed.query, opts.data);
parsed.path = parsed.pathname + '?' + querystring.stringify(parsed.query);
opts.data = null;
}
return yield* this.request('GET', parsed, opts);
}

* post(url, opts) {
opts.data = JSON.stringify(opts.data);
return yield* this.request('POST', url, opts);
}
}

module.exports = Base;
Loading

0 comments on commit 2a77e52

Please sign in to comment.