Skip to content

Commit

Permalink
Merge pull request #9 from cloudnode-pro/patch/browser
Browse files Browse the repository at this point in the history
Include credentials in `fetch()` if operation requires token
  • Loading branch information
williamd5 authored Nov 2, 2022
2 parents 59855fc + 2f264a9 commit 0fc8634
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 8 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ const newsletter = await cloudnode.newsletter.get("newsletter_123asd");
```

#### Browser
Coming soon!
Download the browser SDK from `browser/Cloudnode.js` or use our hosted version.
```html
<script src="https://cloudnode.pro/assets/js/sdk.min.js"></script>
const cloudnode = new Cloudnode();

// get a newsletter
const newsletter = await cloudnode.newsletter.get("newsletter_123asd");
```
> **Warning**: You most likely don't want to set your private token in a public front-end website, as this will allow anyone who sees your front-end JavaScript code to use it for possibly malicious purposes. We advise you use a back-end server to proxy requests to our API, so you do not expose your token to the public.
### TypeScript
```ts
Expand Down
10 changes: 9 additions & 1 deletion README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ const newsletter = await {{config.instanceName}}.newsletter.get("newsletter_123a
```

#### Browser
Coming soon!
Download the browser SDK from `browser/{{config.name}}.js` or use our hosted version.
```html
<script src="{{{config.browserSdkUrl}}}"></script>
const {{config.instanceName}} = new {{config.name}}();

// get a newsletter
const newsletter = await {{config.instanceName}}.newsletter.get("newsletter_123asd");
```
> **Warning**: You most likely don't want to set your private token in a public front-end website, as this will allow anyone who sees your front-end JavaScript code to use it for possibly malicious purposes. We advise you use a back-end server to proxy requests to our API, so you do not expose your token to the public.
### TypeScript
```ts
Expand Down
4 changes: 3 additions & 1 deletion browser/Cloudnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ class Cloudnode {
options.body = JSON.stringify(body);
options.headers["Content-Type"] = "application/json";
}
if (this.#token)
if (this.#token && operation.token !== undefined)
options.headers["Authorization"] = `Bearer ${this.#token}`;
if (operation.token !== undefined)
options.credentials = "include";
const response = await fetch(url.toString(), options);
if (response.status === 204)
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion browser/Cloudnode.min.js

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

5 changes: 5 additions & 0 deletions gen/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ export interface Config {
* Version of the API this client was generated for
*/
apiVersion: string;

/**
* Link to hosted browser SDK
*/
browserSdkUrl: string;
}
3 changes: 2 additions & 1 deletion gen/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"name": "Cloudnode",
"instanceName": "cloudnode",
"baseUrl": "https://api.cloudnode.pro/v5/",
"apiVersion": "5.6.2"
"apiVersion": "5.6.2",
"browserSdkUrl": "https://cloudnode.pro/assets/js/sdk.min.js"
}
4 changes: 3 additions & 1 deletion gen/templates/main.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ class {{config.name}} {
options.body = JSON.stringify(body);
options.headers["Content-Type"] = "application/json";
}
if (this.#token)
if (this.#token && operation.token !== undefined)
options.headers["Authorization"] = `Bearer ${this.#token}`;
if (operation.token !== undefined)
options.credentials = "include";
const response = await fetch(url.toString(), options);
if (response.status === 204) return undefined as any;
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
Expand Down
4 changes: 3 additions & 1 deletion src/Cloudnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ class Cloudnode {
options.body = JSON.stringify(body);
options.headers["Content-Type"] = "application/json";
}
if (this.#token)
if (this.#token && operation.token !== undefined)
options.headers["Authorization"] = `Bearer ${this.#token}`;
if (operation.token !== undefined)
options.credentials = "include";
const response = await fetch(url.toString(), options);
if (response.status === 204)
return undefined;
Expand Down
4 changes: 3 additions & 1 deletion src/Cloudnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ class Cloudnode {
options.body = JSON.stringify(body);
options.headers["Content-Type"] = "application/json";
}
if (this.#token)
if (this.#token && operation.token !== undefined)
options.headers["Authorization"] = `Bearer ${this.#token}`;
if (operation.token !== undefined)
options.credentials = "include";
const response = await fetch(url.toString(), options);
if (response.status === 204) return undefined as any;
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
Expand Down

0 comments on commit 0fc8634

Please sign in to comment.