Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
3117619e
Unverified
Commit
3117619e
authored
May 16, 2023
by
crazywoola
Committed by
GitHub
May 16, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Feature/add test to nodejs sdk (#31)
parent
f5b2271c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
87 additions
and
2 deletions
+87
-2
babel.config.json
sdks/nodejs-client/babel.config.json
+5
-0
index.js
sdks/nodejs-client/index.js
+2
-2
index.test.js
sdks/nodejs-client/index.test.js
+66
-0
package.json
sdks/nodejs-client/package.json
+14
-0
No files found.
sdks/nodejs-client/babel.config.json
0 → 100644
View file @
3117619e
{
"presets"
:
[
"@babel/preset-env"
]
}
\ No newline at end of file
sdks/nodejs-client/index.js
View file @
3117619e
import
axios
from
'axios'
const
BASE_URL
=
'https://api.dify.ai/v1'
export
const
BASE_URL
=
'https://api.dify.ai/v1'
const
routes
=
{
export
const
routes
=
{
application
:
{
method
:
'GET'
,
url
:
()
=>
`/parameters`
...
...
sdks/nodejs-client/index.test.js
0 → 100644
View file @
3117619e
import
{
DifyClient
,
BASE_URL
,
routes
}
from
"."
;
import
axios
from
'axios'
jest
.
mock
(
'axios'
)
describe
(
'Client'
,
()
=>
{
let
difyClient
beforeEach
(()
=>
{
difyClient
=
new
DifyClient
(
'test'
)
})
test
(
'should create a client'
,
()
=>
{
expect
(
difyClient
).
toBeDefined
();
})
// test updateApiKey
test
(
'should update the api key'
,
()
=>
{
difyClient
.
updateApiKey
(
'test2'
);
expect
(
difyClient
.
apiKey
).
toBe
(
'test2'
);
})
});
describe
(
'Send Requests'
,
()
=>
{
let
difyClient
beforeEach
(()
=>
{
difyClient
=
new
DifyClient
(
'test'
)
})
afterEach
(()
=>
{
jest
.
resetAllMocks
()
})
it
(
'should make a successful request to the application parameter'
,
async
()
=>
{
const
method
=
'GET'
const
endpoint
=
routes
.
application
.
url
const
expectedResponse
=
{
data
:
'response'
}
axios
.
mockResolvedValue
(
expectedResponse
)
await
difyClient
.
sendRequest
(
method
,
endpoint
)
expect
(
axios
).
toHaveBeenCalledWith
({
method
,
url
:
`
${
BASE_URL
}${
endpoint
}
`
,
data
:
null
,
params
:
null
,
headers
:
{
Authorization
:
`Bearer
${
difyClient
.
apiKey
}
`
,
'Content-Type'
:
'application/json'
,
},
responseType
:
'json'
,
})
})
it
(
'should handle errors from the API'
,
async
()
=>
{
const
method
=
'GET'
const
endpoint
=
'/test-endpoint'
const
errorMessage
=
'Request failed with status code 404'
axios
.
mockRejectedValue
(
new
Error
(
errorMessage
))
await
expect
(
difyClient
.
sendRequest
(
method
,
endpoint
)).
rejects
.
toThrow
(
errorMessage
)
})
})
\ No newline at end of file
sdks/nodejs-client/package.json
View file @
3117619e
...
...
@@ -14,7 +14,21 @@
"<crazywoola> <<427733928@qq.com>> (https://github.com/crazywoola)"
],
"license"
:
"MIT"
,
"scripts"
:
{
"test"
:
"jest"
},
"jest"
:
{
"transform"
:
{
"^.+
\\
.[t|j]sx?$"
:
"babel-jest"
}
},
"dependencies"
:
{
"axios"
:
"^1.3.5"
},
"devDependencies"
:
{
"@babel/core"
:
"^7.21.8"
,
"@babel/preset-env"
:
"^7.21.5"
,
"babel-jest"
:
"^29.5.0"
,
"jest"
:
"^29.5.0"
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment