Skip to content

Commit

Permalink
Merge d618c3c into 144d55b
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Pollock committed Nov 3, 2018
2 parents 144d55b + d618c3c commit 39c1788
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 166 deletions.
273 changes: 143 additions & 130 deletions .idea/workspace.xml

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion __tests__/apiClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ describe('Makes requests', () => {

expect(fetch.mock.calls).toHaveLength(1);
});

it( 'sets CORS mode', () => {
const formsClient = new ApiClient('/forms', {});
formsClient.setCorsMode(true);
expect(formsClient.corsMode).toBe(true);

formsClient.setCorsMode(false);
expect(formsClient.corsMode).toBe(false);

formsClient.setCorsMode(true);
expect(formsClient.corsMode).toBe(true);
});
});

describe('PUT', () => {
Expand Down Expand Up @@ -159,4 +171,4 @@ describe('Makes requests', () => {
});
});

});
});
5 changes: 2 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,13 @@
<div class="manual-card-wrap" data-ice="cards">
<div class="manual-card">
<div data-ice="card"><h1>caldera-api-client</h1><p>JavaScript API client for <a href="https://calderaforms.com/doc/caldera-forms-rest-api/">Caldera Forms REST API</a> and Caldera Forms Pro REST API.</p><p>&#x1F30B; &#x1F440; <a href="https://calderalabs.org/caldera-api-client/">Documentation</a></p><p><a href="https://travis-ci.org/CalderaWP/caldera-api-client.svg"><img src="https://travis-ci.org/CalderaWP/caldera-api-client.svg?branch=master" alt="Build Status"></a>
<a href="https://coveralls.io/github/CalderaWP/caldera-api-client?branch=master"><img src="https://coveralls.io/repos/github/CalderaWP/caldera-api-client/badge.svg?branch=master" alt="Coverage Status"></a>
<a href="https://calderalabs.org/caldera-api-client/source.html"><img src="https://calderalabs.org/caldera-api-client/badge.svg" alt="Documentation Status"></a>
<a href="https://travis-ci.org/CalderaWP/caldera-api-client.svg"><img src="https://travis-ci.org/CalderaWP/caldera-api-client.svg?branch=master" alt="Documentation Status"></a>
<a href="https://codeclimate.com/github/CalderaWP/caldera-api-client/maintainability"><img src="https://api.codeclimate.com/v1/badges/a2e2c1afbb3eae8a6635/maintainability" alt="Maintainability"></a></p><h2>Install</h2><p><code>npm i @caldera-labs/api-client</code></p><h2>Usage</h2><p>Create a form client and use it to get page one of form configs via API</p><pre><code class="lang-js"><code class="source-code prettyprint">import {wpClientFactory} from &apos;@caldera-labs/api-client&apos;;

const formsAdminApiClient = wpClientFactory(
&apos;https://hiroy.club/wp-json/cf-api/v2&apos;, //root of Caldera Forms REST API namespace
&apos;12345&apos;, //the nonce for REST API cookie authentication
&apos;forms&apos; //type of client to get
&apos;forms&apos; //type of client to get
);
let forms = [];
formsAdminApiClient(page).then(r =&gt; {
Expand Down
32 changes: 16 additions & 16 deletions docs/index.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions docs/manual/README.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@
<div class="content" data-ice="content"><div class="github-markdown" data-ice="content"><h1 id="caldera-api-client">caldera-api-client</h1><p>JavaScript API client for <a href="https://calderaforms.com/doc/caldera-forms-rest-api/">Caldera Forms REST API</a> and Caldera Forms Pro REST API.</p>
<p>&#x1F30B; &#x1F440; <a href="https://calderalabs.org/caldera-api-client/">Documentation</a></p>
<p><a href="https://travis-ci.org/CalderaWP/caldera-api-client.svg"><img src="https://travis-ci.org/CalderaWP/caldera-api-client.svg?branch=master" alt="Build Status"></a>
<a href="https://coveralls.io/github/CalderaWP/caldera-api-client?branch=master"><img src="https://coveralls.io/repos/github/CalderaWP/caldera-api-client/badge.svg?branch=master" alt="Coverage Status"></a>
<a href="https://calderalabs.org/caldera-api-client/source.html"><img src="https://calderalabs.org/caldera-api-client/badge.svg" alt="Documentation Status"></a>
<a href="https://travis-ci.org/CalderaWP/caldera-api-client.svg"><img src="https://travis-ci.org/CalderaWP/caldera-api-client.svg?branch=master" alt="Documentation Status"></a>
<a href="https://codeclimate.com/github/CalderaWP/caldera-api-client/maintainability"><img src="https://api.codeclimate.com/v1/badges/a2e2c1afbb3eae8a6635/maintainability" alt="Maintainability"></a></p>
<h2 id="install">Install</h2><p><code>npm i @caldera-labs/api-client</code></p>
<h2 id="usage">Usage</h2><p>Create a form client and use it to get page one of form configs via API</p>
Expand All @@ -88,7 +87,7 @@ <h2 id="usage">Usage</h2><p>Create a form client and use it to get page one of f
const formsAdminApiClient = wpClientFactory(
&apos;https://hiroy.club/wp-json/cf-api/v2&apos;, //root of Caldera Forms REST API namespace
&apos;12345&apos;, //the nonce for REST API cookie authentication
&apos;forms&apos; //type of client to get
&apos;forms&apos; //type of client to get
);
let forms = [];
formsAdminApiClient(page).then(r =&gt; {
Expand Down
5 changes: 2 additions & 3 deletions docs/manual/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,13 @@
<div class="manual-card-wrap" data-ice="cards">
<div class="manual-card">
<div data-ice="card"><h1>caldera-api-client</h1><p>JavaScript API client for <a href="https://calderaforms.com/doc/caldera-forms-rest-api/">Caldera Forms REST API</a> and Caldera Forms Pro REST API.</p><p>&#x1F30B; &#x1F440; <a href="https://calderalabs.org/caldera-api-client/">Documentation</a></p><p><a href="https://travis-ci.org/CalderaWP/caldera-api-client.svg"><img src="https://travis-ci.org/CalderaWP/caldera-api-client.svg?branch=master" alt="Build Status"></a>
<a href="https://coveralls.io/github/CalderaWP/caldera-api-client?branch=master"><img src="https://coveralls.io/repos/github/CalderaWP/caldera-api-client/badge.svg?branch=master" alt="Coverage Status"></a>
<a href="https://calderalabs.org/caldera-api-client/source.html"><img src="https://calderalabs.org/caldera-api-client/badge.svg" alt="Documentation Status"></a>
<a href="https://travis-ci.org/CalderaWP/caldera-api-client.svg"><img src="https://travis-ci.org/CalderaWP/caldera-api-client.svg?branch=master" alt="Documentation Status"></a>
<a href="https://codeclimate.com/github/CalderaWP/caldera-api-client/maintainability"><img src="https://api.codeclimate.com/v1/badges/a2e2c1afbb3eae8a6635/maintainability" alt="Maintainability"></a></p><h2>Install</h2><p><code>npm i @caldera-labs/api-client</code></p><h2>Usage</h2><p>Create a form client and use it to get page one of form configs via API</p><pre><code class="lang-js"><code class="source-code prettyprint">import {wpClientFactory} from &apos;@caldera-labs/api-client&apos;;

const formsAdminApiClient = wpClientFactory(
&apos;https://hiroy.club/wp-json/cf-api/v2&apos;, //root of Caldera Forms REST API namespace
&apos;12345&apos;, //the nonce for REST API cookie authentication
&apos;forms&apos; //type of client to get
&apos;forms&apos; //type of client to get
);
let forms = [];
formsAdminApiClient(page).then(r =&gt; {
Expand Down
15 changes: 14 additions & 1 deletion manual/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ const formsAdminApiClient = calderaApiClient.wpClientFactory(
);
```

#### Enabling Cross-Origin Requests
By default, requests are same-origin only. Cross-origin requests are possible. The WP REST API nonce auth will not work cross-origin. You must implement your own auth.
```js
import {wpClientFactory} from '@caldera-labs/api-client';
const formsAdminApiClient = calderaApiClient.wpClientFactory(
'https://hiroy.club/wp-json/cf-api/v2',
'12345',
'forms'
);
formsAdminApiClient.setCorsMode(true);
```


### WordPress Admin Clients

* Get or update Caldera Forms form configs or other settings.
Expand Down Expand Up @@ -234,4 +247,4 @@ describe('Forms API', () => {
#### Helpful Links About Fetch and Testing With Fetch
* [MDN Fetch docs](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
* [David Walsh blog post about Fetch](https://davidwalsh.name/fetch)
* [Why tests using fetch mocks must catch errors](https://github.com/jefflau/jest-fetch-mock/issues/60#issuecomment-402489882)
* [Why tests using fetch mocks must catch errors](https://github.com/jefflau/jest-fetch-mock/issues/60#issuecomment-402489882)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@caldera-labs/api-client",
"version": "0.5.0",
"version": "0.6.0",
"description": "API client for Caldera Forms and Caldera Forms Pro",
"main": "./dist/index.js",
"scripts": {
Expand Down
47 changes: 39 additions & 8 deletions src/ApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ function removeForwardSlash(endpoint) {
return endpoint;
}

function createRequestArgs(method: string, headers: Headers,corsMode:boolean = true ){
const args = {
method,
redirect: 'follow',
headers
};

if( corsMode ){
return {
...args,
mode:'cors',
}
}else{
return {
...args,
mode:'same-origin',
credentials: 'same-origin',
}
}
};

//@flow
/**
* Generic API client
Expand All @@ -23,6 +44,13 @@ export class ApiClient{
*/
headers: Headers;

/**
* Is CORS mode enabled?
*
* @type string
*/
corsMode: boolean;

/**
*
* @param {String} route The full URL of API route is a client for
Expand All @@ -45,6 +73,14 @@ export class ApiClient{
this.headers = newHeaders;
}

/**
* Set CORS mode
*
* @param {boolean} corsMode
*/
setCorsMode(corsMode: boolean){
this.corsMode = corsMode;
}
/**
* Make a request to an endpoint
*
Expand All @@ -68,13 +104,7 @@ export class ApiClient{
* @returns {Request}
*/
createRequest(endpoint: string, data: Object, method: string): Request {
let args = {
method,
mode:'same-origin',
credentials: 'same-origin',
redirect: 'follow',
headers: this.headers
};
let args = createRequestArgs(method,this.headers,this.corsMode);

if ('POST' === method || 'PUT' === method) {
args.body = JSON.stringify(data);
Expand All @@ -85,6 +115,7 @@ export class ApiClient{
return new Request(this.urlFromEndpoint(endpoint, method,data), args);

}

/**
* Create URL for route with endpoint
* @param endpoint
Expand Down Expand Up @@ -223,4 +254,4 @@ export class ApiClient{
return this.route + '?' + str;
}

}
}

0 comments on commit 39c1788

Please sign in to comment.