Skip to content

Commit c784f72

Browse files
committed
moved auth api request to backend + frontend cleanup
1 parent 5ce2ac4 commit c784f72

File tree

7 files changed

+25
-30
lines changed

7 files changed

+25
-30
lines changed

admin/src/components/Input/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import React, { useState } from 'react';
22
import { useIntl } from 'react-intl';
33
import { TextInput } from '@strapi/design-system/TextInput';
44
import { Stack } from '@strapi/design-system/Stack';
5-
import { Box } from '@strapi/design-system/Box';
65
import { Button } from '@strapi/design-system/Button';
76
import { Textarea } from '@strapi/design-system';
8-
import { TwoColsLayout } from '@strapi/design-system';
7+
import { auth } from '@strapi/helper-plugin'
98

109

1110
export default function Index({
@@ -18,17 +17,16 @@ export default function Index({
1817
attribute,
1918
}) {
2019
const { formatMessage } = useIntl();
21-
const [content, setContent] = useState('');
2220
const [prompt, setPrompt] = useState('');
2321
const [err, setErr] = useState('');
2422

25-
const aiClick = async () => {
23+
const generateText = async () => {
2624
try {
27-
const response = await fetch('https://api.openai.com/v1/completions', {
25+
const response = await fetch(`/ai-text-generation/generate-text`, {
2826
method: 'POST',
2927
headers: {
3028
'Content-Type': 'application/json',
31-
'Authorization': `Bearer ${accessToken}` //functionality to be added
29+
'Authorization': `Bearer ${auth.getToken()}`
3230
},
3331
body: JSON.stringify({
3432
'model': 'text-davinci-001',
@@ -46,7 +44,9 @@ export default function Index({
4644
}
4745

4846
const result = await response.json();
49-
onChange({ target: { name, value: result.choices[0].text, type: attribute.type } })
47+
const parsedResult = result.choices[0].text.replace(/(?:\r\n|\r|\n)/g, '');
48+
49+
onChange({ target: { name, value: parsedResult, type: attribute.type } })
5050
} catch (err) {
5151
setErr(err.message);
5252
}
@@ -80,7 +80,7 @@ export default function Index({
8080
{value}
8181
</Textarea>
8282
<Stack horizontal spacing={4}>
83-
<Button onClick={aiClick}>Generate</Button>
83+
<Button onClick={() => generateText()}>Generate</Button>
8484
<Button onClick={() => clearGeneratedText()}>Clear</Button>
8585
</Stack>
8686
</Stack>

admin/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default {
4848
id: "ai-text-generation.text-ai.description",
4949
defaultMessage: "Let AI do your writing!",
5050
},
51-
icon: AITextIcon, // don't forget to create/import your icon component
51+
icon: PluginIcon, // don't forget to create/import your icon component
5252
components: {
5353
Input: async () => import(/* webpackChunkName: "input-component" */ "./components/Input"),
5454
},

server/controllers/my-controller.js renamed to server/controllers/ai-controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = ({ strapi }) => ({
44
async generate(ctx) {
55
ctx.body = await strapi
66
.plugin('ai-text-generation')
7-
.service('myService')
8-
.generate(ctx.request.body.prompt);
7+
.service('openAi')
8+
.generateText(ctx.request.body.prompt);
99
},
1010
});

server/controllers/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
const myController = require('./my-controller');
3+
const aiController = require('./ai-controller');
44

55
module.exports = {
6-
myController,
6+
aiController,
77
};

server/routes/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module.exports = [
22
{
33
method: 'POST',
4-
path: '/generate',
5-
handler: 'myController.generate',
4+
path: '/generate-text',
5+
handler: 'aiController.generate',
66
config: {
77
policies: [],
88
},

server/services/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
const myService = require('./my-service');
3+
const openAi = require('./open-ai');
44

55
module.exports = {
6-
myService,
6+
openAi,
77
};

server/services/my-service.js renamed to server/services/open-ai.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22

33
const axios = require('axios');
44

5-
65
module.exports = ({ strapi }) => ({
7-
getWelcomeMessage() {
8-
return 'Welcome to Strapi 🚀';
9-
},
106

11-
async generate(prompt) {
7+
async generateText(prompt) {
128
try {
139
const response = await axios(
1410
{
1511
url: 'https://api.openai.com/v1/completions',
1612
method: 'POST',
1713
headers: {
1814
'Content-Type': 'application/json',
19-
'Authorization': `Bearer ${strapi.plugin('ai-text-generation').config('accessToken')}` //functionality to be added
15+
'Authorization': `Bearer ${strapi.plugin('ai-text-generation').config('apiToken')}`
2016
},
2117
data: JSON.stringify({
2218
'model': 'text-davinci-001',
@@ -29,15 +25,14 @@ module.exports = ({ strapi }) => ({
2925
})
3026
})
3127

32-
28+
3329
const result = await response.data;
34-
console.log('response:', response.data)
35-
return result;
36-
}
37-
catch (err){
30+
return result;
31+
}
32+
catch (err) {
3833
console.log(err.response)
3934
}
4035

41-
}
42-
36+
}
37+
4338
});

0 commit comments

Comments
 (0)