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

Update/improve request parameters #210

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@ dependencies:
- npm install -g npm@3.8.6
machine:
node:
version: 5.11.1
test:
override:
- DEBUG=wpcom* make test
version: 5.11.1
41 changes: 28 additions & 13 deletions lib/util/send-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import debugFactory from 'debug';
const debug = debugFactory( 'wpcom:send-request' );
const debug_res = debugFactory( 'wpcom:send-request:res' );

export const apiVersionPattern = /^(?:(\d+)\.)?(\d+)$/;
export const apiNamespacePattern = /^(?:([a-z]+)\/)?v(\d+)$/;

/**
* Request to WordPress REST API
*
Expand Down Expand Up @@ -40,21 +43,33 @@ export default function sendRequest( params, query, body, fn ) {
// query could be `null`
query = query || {};

// Handle special query parameters
// - `apiVersion`
if ( query.apiVersion ) {
params.apiVersion = query.apiVersion;
debug( 'apiVersion: %o', params.apiVersion );
delete query.apiVersion;
const { apiVersion, apiNamespace } = query;

if ( typeof apiVersion !== 'undefined' && typeof apiNamespace !== 'undefined' ) {
throw new Error( 'apiVersion and apiNamespace cannot be simultaneously defined.' );
}

if ( typeof apiVersion === 'undefined' ) {
if ( typeof apiNamespace === 'undefined' ) {
// set apiVersion default value
params.apiVersion = this.apiVersion;
} else {
params.apiNamespace = apiNamespace;
delete query.apiNamespace;
}
} else {
params.apiVersion = this.apiVersion;
params.apiVersion = apiVersion;
delete query.apiVersion;
}

// check apiVersion shape
if ( params.apiVersion && ! apiVersionPattern.test( params.apiVersion ) ) {
throw new Error( `'{ apiVersion: '${ params.apiVersion }' } value is invalid.` );
}

// - `apiNamespace`
if ( query.apiNamespace ) {
params.apiNamespace = query.apiNamespace;
debug( 'apiNamespace: %o', params.apiNamespace );
delete query.apiNamespace;
// check apiNamespace shape`
if ( params.apiNamespace && ! apiNamespacePattern.test( params.apiNamespace ) ) {
throw new Error( `'{ apiNamespace: '${ params.apiNamespace }' } value is invalid.` );
}

// - `proxyOrigin`
Expand Down Expand Up @@ -91,4 +106,4 @@ export default function sendRequest( params, query, body, fn ) {
err ? reject( err ) : resolve( res );
} );
} );
};
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
],
"dependencies": {
"babel-runtime": "^6.9.2",
"chai": "^3.5.0",
"debug": "^2.2.0",
"qs": "^4.0.0",
"wpcom-xhr-request": "1.0.0"
Expand Down
85 changes: 85 additions & 0 deletions test/test.util.send-request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Module dependencies
*/
import { expect, assert } from 'chai';

/**
* Internal dependencies
*/
import wpcomFactory, {
defaultApiVersion
} from '../';

import sendRequest, {
apiVersionPattern,
apiNamespacePattern,
}
from '../lib/util/send-request';

const wpcom = wpcomFactory();

/**
* Testing data
*/
import fixture from './fixture';

describe( 'wpcom', () => {
describe( 'wpcom.util.sendRequest', () => {
describe( 'apiVersion shape', () => {
it( 'should not accept `v1` as a valid version value', () => {
expect( apiVersionPattern.test( 'v1' ) ).to.be.false;
} );

it( 'should accept `1` as a valid version value', () => {
expect( apiVersionPattern.test( '1' ) ).to.be.true;
} );

it( 'should not accept `1.` as a valid version value', () => {
expect( apiVersionPattern.test( '1.' ) ).to.be.false;
} );

it( 'should accept `1.3` as a valid version value', () => {
expect( apiVersionPattern.test( '1.3' ) ).to.be.true;
} );
} );

describe( 'apiNamespace shape', () => {
it( 'should not accept `wpcom` as a valid version value', () => {
expect( apiNamespacePattern.test( 'wpcom' ) ).to.be.false;
} );

it( 'should accept `wpcom/v1` as a valid version value', () => {
expect( apiNamespacePattern.test( 'wpcom/v1' ) ).to.be.true;
} );

it( 'should not accept `wpcom/` as a valid version value', () => {
expect( apiNamespacePattern.test( 'wpcom/' ) ).to.be.false;
} );

it( 'should not accept `wpcom/1` as a valid version value', () => {
expect( apiNamespacePattern.test( 'wpcom/1' ) ).to.be.false;
} );
} );

describe( 'parameters parameters', () => {
it( 'should throw Error when `apiVersion` is malformed', () => {
const path = '/endpoint/path';
const promise = sendRequest.bind( wpcom, path, { apiVersion: 'version1' }, {} );
assert.throws( promise, Error, '{ apiVersion: \'version1\' } value is invalid.' );
} );

it( 'should throw Error when `apiNamespace` is malformed', () => {
const path = '/endpoint/path';
const promise = sendRequest.bind( wpcom, path, { apiNamespace: 'wpcom' }, {} );
assert.throws( promise, Error, '{ apiNamespace: \'wpcom\' } value is invalid.' );
} );

it( 'should throw Error when `apiVersion` and `apiNamespace` are both defined', () => {
const path = '/timezones';

const promise = sendRequest.bind( wpcom, path, { apiVersion: 'v1.4', apiNamespace: 'wpcom/v2' }, {} );
assert.throws( promise, Error, 'apiVersion and apiNamespace cannot be simultaneously defined.' );
} );
} );
} );
} );
54 changes: 0 additions & 54 deletions test/test.wpcom.site.follow.js

This file was deleted.

103 changes: 0 additions & 103 deletions test/test.wpcom.site.taxonomy.term.js

This file was deleted.

7 changes: 1 addition & 6 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import fixture from './fixture';
var configFactory;

try {
configFactory = require( './config' );
configFactory = require( './config' ).default;
} catch ( ex ) {
configFactory = {};
}
Expand All @@ -27,17 +27,13 @@ const env = isClientSide && (
? 'production'
: 'development';

console.log( 'environment: %j', env );

const config = configFactory[ env ] || {};

if ( isClientSide ) {
const clientId = config.oauth.client_id;
console.log( 'clientId: %o', clientId );

qryString = qs.parse( document.location.search.replace( /^\?/, '' ) );
reqHandler = qryString.handler || 'wpcom-proxy-request';
console.log( `reqHandler: %o`, reqHandler );

if (
'wpcom-xhr-request' === reqHandler ||
Expand Down Expand Up @@ -73,7 +69,6 @@ function wpcom() {
reqHandler = qryString.handler || 'wpcom-proxy-request';

if ( 'wpcom-proxy-request' === reqHandler ) {
console.log( 'PROXY request handler' );
let proxy = require( 'wpcom-proxy-request' );
_wpcom = wpcomFactory( proxy );
_wpcom.request( {
Expand Down