Skip to content

Commit a1d6843

Browse files
committed
refactor: changed the way we handle api key and added a proxy mode
BREAKING CHANGES: You have to remove your api key from the `forRoot` call and use proxy url if you want to use one.
1 parent b1d8e39 commit a1d6843

File tree

6 files changed

+202
-164
lines changed

6 files changed

+202
-164
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Add `XivapiClientModule` to your `AppModule` imports:
2424
],
2525
imports: [
2626
...
27-
XivapiClientModule.forRoot('my-api-key'),
27+
XivapiClientModule.forRoot(),
2828
...
2929
],
3030
bootstrap: [AppComponent]
@@ -50,3 +50,40 @@ export class FooComponent {
5050
}
5151
}
5252
```
53+
54+
## Use with private_key (Google Cloud Function)
55+
56+
- Create a google cloud function, name it as you want.
57+
- Insert following code (with your key):
58+
```js
59+
exports.xivapiProxy = (req, res) => {
60+
let request = require('request');
61+
const apiKey = '<your api key>>';
62+
res.set('Access-Control-Allow-Origin', '<your allowed origins>')
63+
.set('Access-Control-Allow-Headers', 'Content-Type');
64+
const url = Buffer.from(req.query.url, 'base64').toString();
65+
request(
66+
{
67+
url:`${url}${url.indexOf('?') > -1 ? '&':'?'}private_key=${apiKey}`,
68+
},
69+
function(error, response, body) {
70+
let errorBody = JSON.parse(body);
71+
let errorMessage = errorBody.error || errorBody.message;
72+
if (error || errorMessage) {
73+
res.status(400).send(errorMessage || 'Unknown Error');
74+
} else {
75+
res.status(200).set('Content-Type', 'application/json').send(body);
76+
}
77+
});
78+
};
79+
```
80+
- Enable proxy mode inside the api module:
81+
```ts
82+
XivapiClientModule.forRoot('<GCF trigger url>')
83+
```
84+
85+
Example url: https://us-central1-myproject.cloudfunctions.net/xivapi-proxy
86+
87+
Keep in mind that you can enable it based on the current environment, simply give `null` as proxy url if environment isn't prod,
88+
to use "normal" mode in order to get better debugging
89+
when using dev environment.

0 commit comments

Comments
 (0)