Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
New version v0.12.0!
Browse files Browse the repository at this point in the history
Rafael Fakhreev committed Jul 7, 2017

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent 5a0a2d0 commit d4da9e2
Showing 9 changed files with 211 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Apipie
(Previously called VueApify )
Thanks [Akiyamka](https://github.com/akiyamka) for new project name!! (Previously called VueApify)

This is a tool for transforming the declaration of REST Api to js object.
Inspired by VueRouter, koa2 and axios.
48 changes: 34 additions & 14 deletions dist/apipie.esm.js

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

2 changes: 1 addition & 1 deletion dist/apipie.esm.js.map

Large diffs are not rendered by default.

48 changes: 34 additions & 14 deletions dist/apipie.js

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

2 changes: 1 addition & 1 deletion dist/apipie.js.map

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions lib/create-rest-tree.js
Original file line number Diff line number Diff line change
@@ -37,7 +37,13 @@ export function addTreeBranch (branch, record, indexPath, closurePack) {
branch[record.name] = {}
if (record.children && record.children.length) {
if (record.method) {
record.children.push({ name: record.method, method: record.method, url: record.url })
record.children.push({
name: record.method,
method: record.method,
url: record.url,
data: !!record.data,
params: !!record.params
})
}
record.children.forEach((childRecord, index) =>
addTreeBranch(branch[record.name], childRecord, indexPath.concat(index), closurePack))
@@ -75,6 +81,10 @@ export function normalizeRecord (record, props) {
const { options = [], meta = [], hooks = [] } = props
const normalizedRecord = {
_normalized: true,
_require: {
data: !!record.data,
params: !!record.params
},
name: record.name,
meta: [].concat(meta, record.meta || {}),
options: [].concat(options, record.options || {}),
@@ -133,7 +143,7 @@ export function createExecFunc (record, fullName, axios) {
if (record.meta instanceof Array) {
record.meta = merge.all(record.meta)
}
const tmpOptions = merge(record.options, parseExecArgs(record.options.url, props))
const tmpOptions = merge(record.options, parseExecArgs(record.options.url, props, record))
record.hooks.push(createRequestFunc())

const fn = compose(record.hooks)
20 changes: 12 additions & 8 deletions lib/utils/args-parser.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import pathToRegexp from 'path-to-regexp'

export default function parseExecArgs (url, props) {
export default function parseExecArgs (url, props, { _require }) {
const result = { url }
if (_require.params && (!props || !props.params)) throw new Error('Require params!')
if (_require.data && (!props || !props.data)) throw new Error('Require data!')
let requireParams = pathToRegexp.parse(url)
.filter(token => typeof token !== 'string' )
.map(({ name }) => name)
if (requireParams.length && !props) throw new Error('Require url_params!')
if (!props) {
return result
}
const { url_params, params, data } = props
if (url_params) {
/*
TODO: add validations of url_params
let names = pathToRegexp.parse(url)
.filter(token => typeof token !== 'string' )
.map(({ name }) => name)
names === Object.keys(url_params)
*/
requireParams.forEach(param => {
if (!url_params[param]) {
throw new Error(`Require ${requireParams.join(', ')}, but given ${Object.keys(url_params).join(', ') || 'nothing'}`)
}
})
const toPath = pathToRegexp.compile(url)
result.url = toPath(url_params)
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apipie",
"version": "0.11.0",
"version": "0.12.0",
"description": "Transform REST API declaration to js object",
"main": "dist/apipie.js",
"module": "dist/apipie.esm.js",
115 changes: 115 additions & 0 deletions tests/create-rest-tree.test.js
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ describe('Create REST Api routing', () => {
const props = {}
const expected = {
_normalized: true,
_require: { data: false, params: false },
name: 'test',
meta: [{}],
options: [{ url: 'url', method: 'get' }],
@@ -25,6 +26,7 @@ describe('Create REST Api routing', () => {
const props = {}
const expected = {
_normalized: true,
_require: { data: false, params: false },
name: 'test',
meta: [{}],
options: [{}, { url: 'url', method: 'get' }],
@@ -38,6 +40,7 @@ describe('Create REST Api routing', () => {
const props = {}
const expected = {
_normalized: true,
_require: { data: false, params: false },
name: 'test',
meta: [{}],
options: [{}],
@@ -60,6 +63,7 @@ describe('Create REST Api routing', () => {
}
const expected = {
_normalized: true,
_require: { data: false, params: false },
name: 'test',
meta: [{ props: 'props', test: "123" }, { test: 'test' }],
options: [{ props: 'props' }, { test: 'test' }, { url: 'url', method: 'get' }],
@@ -73,6 +77,7 @@ describe('Create REST Api routing', () => {
const axiosMock = () => Promise.resolve({ success: true })
test('Basic', () => {
const record = {
_require: { data: false, params: false },
name: 'test',
options: [{}, {
url: '/test/:id',
@@ -99,6 +104,7 @@ describe('Create REST Api routing', () => {
})
test('With data and params', () => {
const record = {
_require: { data: false, params: false },
name: 'test',
options: [{}, {
url: '/test/:id',
@@ -139,6 +145,7 @@ describe('Create REST Api routing', () => {
}
const record1 = {
_normalized: true,
_require: { data: false, params: false },
name: 'test',
meta: [props.meta[0], { test: 'test' }],
options: [props.options[0], { test: 'test' }, { url: '/test/:id', method: 'get' }],
@@ -147,6 +154,7 @@ describe('Create REST Api routing', () => {
}
const record2 = {
_normalized: true,
_require: { data: false, params: false },
name: 'test',
meta: [props.meta[0], { test: 'test' }],
options: [props.options[0], { test: 'test' }, { url: '/test/:id', method: 'get' }],
@@ -234,6 +242,111 @@ describe('Create REST Api routing', () => {
const fn = createExecFunc(normalizeRecord(record, props), ['a'], axiosMock)
return expect(fn()).resolves.toMatchObject({ meta: { test1: false, test2: true } })
})
describe('Test data, params and url_params validations', () => {
const data = true
const params = true
const props = { meta: [{}], options: [{}], hooks: [] }
describe('Test data validation', () => {
test('Valid', () => {
const record = { name: 'test', method: 'get', url: '/test', data }
const expectedCtx = {
meta: {},
options: {
data: 'test_data',
method: 'get',
url: '/test'
},
name: 'test',
fullName: ['test'],
response: { success: true }
}
const normalizedRecord = normalizeRecord(record, props)
const fn = createExecFunc(normalizedRecord, ['test'], axiosMock)
return expect(fn({ data: 'test_data' })).resolves.toEqual(expectedCtx)
})
test('Invalid', () => {
const record = { name: 'test', method: 'get', url: '/test', data }
const normalizedRecord = normalizeRecord(record, props)
const fn = createExecFunc(normalizedRecord, ['test'], axiosMock)
try {
expect.assertions(1)
fn()
} catch (err) {
expect(err.message).toEqual('Require data!')
}
})
})
describe('Test params validation', () => {
test('Valid', () => {
const record = { name: 'test', method: 'get', url: '/test', params }
const expectedCtx = {
meta: {},
options: {
params: { test: 'test_param' },
method: 'get',
url: '/test'
},
name: 'test',
fullName: ['test'],
response: { success: true }
}
const normalizedRecord = normalizeRecord(record, props)
const fn = createExecFunc(normalizedRecord, ['test'], axiosMock)
return expect(fn({ params: { test: 'test_param' }})).resolves.toEqual(expectedCtx)
})
test('Invalid', () => {
const record = { name: 'test', method: 'get', url: '/test', params }
const normalizedRecord = normalizeRecord(record, props)
const fn = createExecFunc(normalizedRecord, ['test'], axiosMock)
try {
expect.assertions(1)
fn()
} catch (err) {
expect(err.message).toEqual('Require params!')
}
})
})
describe('Test url_params validation', () => {
test('Valid', () => {
const record = { name: 'test', method: 'get', url: '/test/:id1/:id2' }
const expectedCtx = {
meta: {},
options: {
method: 'get',
url: '/test/1/2'
},
name: 'test',
fullName: ['test'],
response: { success: true }
}
const normalizedRecord = normalizeRecord(record, props)
const fn = createExecFunc(normalizedRecord, ['test'], axiosMock)
return expect(fn({ url_params: { id1: 1, id2: 2 }})).resolves.toEqual(expectedCtx)
})
test('Invalid all params', () => {
const record = { name: 'test', method: 'get', url: '/test/:id1/:id2' }
const normalizedRecord = normalizeRecord(record, props)
const fn = createExecFunc(normalizedRecord, ['test'], axiosMock)
try {
expect.assertions(1)
fn()
} catch (err) {
expect(err.message).toEqual('Require url_params!')
}
})
test('Invalid with part of url_params', () => {
const record = { name: 'test', method: 'get', url: '/test/:id1/:id2' }
const normalizedRecord = normalizeRecord(record, props)
const fn = createExecFunc(normalizedRecord, ['test'], axiosMock)
try {
expect.assertions(1)
fn({ url_params: { id1: 1 } })
} catch (err) {
expect(err.message).toEqual('Require id1, id2, but given id1')
}
})
})
})
})
describe('calculateBranchNodes', () => {
test('Basic', () => {
@@ -246,6 +359,7 @@ describe('Create REST Api routing', () => {
}]
const expectedRecord = {
_normalized: true,
_require: { data: false, params: false },
name: 'test1',
meta: [ {}, {} ],
options: [ {}, {}, {}, { url: '/test/1', method: 'get' } ],
@@ -280,6 +394,7 @@ describe('Create REST Api routing', () => {
const path = [0, 0]
const expectedRecord = {
_normalized: true,
_require: { data: false, params: false },
name: 'test1',
meta: [ { acc: 'acc' }, { test: 'test' }, { test1: 'test1' } ],
options:

0 comments on commit d4da9e2

Please sign in to comment.