updated the frontend plugin to accept apikey config

Signed-off-by: Matt Mulligan <mmulligan03@gmail.com>
This commit is contained in:
Matt Mulligan
2022-06-22 10:55:28 -04:00
parent 547a573e99
commit ea5631a8b2
4 changed files with 36 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-stack-overflow': patch
---
Added API key configuration to bypass encoding
+10
View File
@@ -15,6 +15,16 @@ stackoverflow:
baseUrl: https://api.stackexchange.com/2.2 # alternative: your internal stack overflow instance
```
### Stack Overflow for Teams (private stack overflow instance)
If you have a private stack overflow instance you will need to supply an API key as well. You can read more about how to set this up by going to [Stack Overflows Help Page](https://stackoverflow.help/en/articles/4385859-stack-overflow-for-teams-api)
```yaml
stackoverflow:
baseUrl: https://api.stackexchange.com/2.2 # alternative: your internal stack overflow instance
apiKey: $STACK_OVERFLOW_API_KEY
```
## Areas of Responsibility
This stack overflow frontend plugin is primarily responsible for the following:
+6
View File
@@ -24,5 +24,11 @@ export interface Config {
* @visibility frontend
*/
baseUrl: string;
/**
* The api key to authenticate to Stack Overflow API
* @visibility backend
*/
apiKey: string;
};
}
@@ -42,6 +42,7 @@ import {
export const Content = (props: StackOverflowQuestionsContentProps) => {
const { requestParams } = props;
const configApi = useApi(configApiRef);
const apiKey = configApi.getOptionalString('stackoverflow.apiKey');
const baseUrl =
configApi.getOptionalString('stackoverflow.baseUrl') ||
'https://api.stackexchange.com/2.2';
@@ -49,8 +50,20 @@ export const Content = (props: StackOverflowQuestionsContentProps) => {
const { value, loading, error } = useAsync(async (): Promise<
StackOverflowQuestion[]
> => {
const params = qs.stringify(requestParams, { addQueryPrefix: true });
const response = await fetch(`${baseUrl}/questions${params}`);
try {
if (Object.keys(requestParams).indexOf('key') >= 0) {
delete requestParams.key;
}
} catch (e) {
// console.log("Failed to remove key from params");
}
const params = qs.stringify(requestParams, {
arrayFormat: 'comma',
addQueryPrefix: true,
});
const apiKeyParam = apiKey ? `${params ? '&' : '?'}key=${apiKey}` : '';
const response = await fetch(`${baseUrl}/questions${params}${apiKeyParam}`);
const data = await response.json();
return data.items;
}, []);