Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Sharkey
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dima Krasner
Sharkey
Commits
bdb59253
Commit
bdb59253
authored
3 years ago
by
syuilo
Browse files
Options
Downloads
Patches
Plain Diff
update api api
parent
3557741f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+2
-10
2 additions, 10 deletions
README.md
src/api.ts
+35
-38
35 additions, 38 deletions
src/api.ts
test/api.ts
+18
-5
18 additions, 5 deletions
test/api.ts
with
55 additions
and
53 deletions
README.md
+
2
−
10
View file @
bdb59253
...
...
@@ -18,21 +18,13 @@ coming soon
todo
## API request
都度インスタンスやトークンを指定する場合
```
ts
import
*
as
Misskey
from
'
misskey-js
'
;
const
meta
=
await
Misskey
.
api
.
request
(
'
https://misskey.test
'
,
'
meta
'
,
{
detail
:
true
},
'
TOKEN
'
);
```
最初にインスタンスやトークンを指定し、以後のリクエストでその情報を使いまわす場合
```
ts
import
*
as
Misskey
from
'
misskey-js
'
;
const
cli
=
new
Misskey
.
api
.
APIClient
({
origin
:
'
https://misskey.test
'
origin
:
'
https://misskey.test
'
,
credential
:
'
TOKEN
'
,
});
cli
.
i
=
{
token
:
'
TOKEN
'
};
const
meta
=
await
cli
.
request
(
'
meta
'
,
{
detail
:
true
});
```
...
...
This diff is collapsed.
Click to expand it.
src/api.ts
+
35
−
38
View file @
bdb59253
...
...
@@ -14,54 +14,51 @@ export function isAPIError(reason: any): reason is APIError {
return
reason
[
MK_API_ERROR
]
===
true
;
}
export
function
request
<
E
extends
keyof
Endpoints
>
(
origin
:
string
,
endpoint
:
E
,
data
:
Endpoints
[
E
][
'
req
'
]
=
{},
credential
:
string
|
null
|
undefined
,
):
Promise
<
Endpoints
[
E
][
'
res
'
]
>
{
const
promise
=
new
Promise
<
Endpoints
[
E
][
'
res
'
]
>
((
resolve
,
reject
)
=>
{
// Append a credential
if
(
credential
!==
undefined
)
(
data
as
Record
<
string
,
any
>
).
i
=
credential
;
// Send request
fetch
(
`
${
origin
}
/api/
${
endpoint
}
`
,
{
method
:
'
POST
'
,
body
:
JSON
.
stringify
(
data
),
credentials
:
'
omit
'
,
cache
:
'
no-cache
'
}).
then
(
async
(
res
)
=>
{
const
body
=
res
.
status
===
204
?
null
:
await
res
.
json
();
if
(
res
.
status
===
200
)
{
resolve
(
body
);
}
else
if
(
res
.
status
===
204
)
{
resolve
(
null
);
}
else
{
reject
({
[
MK_API_ERROR
]:
true
,
...
body
.
error
});
}
}).
catch
(
reject
);
});
return
promise
;
}
export
class
APIClient
{
public
i
:
{
token
:
string
;
}
|
null
=
null
;
private
origin
:
string
;
public
origin
:
string
;
public
credential
:
string
|
null
|
undefined
;
public
fetch
:
(
typeof
fetch
);
constructor
(
opts
:
{
origin
:
APIClient
[
'
origin
'
];
credential
?:
APIClient
[
'
credential
'
];
fetch
?:
APIClient
[
'
fetch
'
]
|
null
|
undefined
;
})
{
this
.
origin
=
opts
.
origin
;
this
.
credential
=
opts
.
credential
;
this
.
fetch
=
opts
.
fetch
||
fetch
;
}
public
request
<
E
extends
keyof
Endpoints
>
(
endpoint
:
E
,
data
:
Endpoints
[
E
][
'
req
'
]
=
{},
credential
?:
string
|
null
|
undefined
,
):
Promise
<
Endpoints
[
E
][
'
res
'
]
>
{
return
request
(
this
.
origin
,
endpoint
,
data
,
credential
===
undefined
?
this
.
i
?.
token
:
credential
);
const
promise
=
new
Promise
<
Endpoints
[
E
][
'
res
'
]
>
((
resolve
,
reject
)
=>
{
// Append a credential
if
(
this
.
credential
)
(
data
as
Record
<
string
,
any
>
).
i
=
this
.
credential
;
if
(
credential
)
(
data
as
Record
<
string
,
any
>
).
i
=
credential
;
// Send request
this
.
fetch
(
`
${
this
.
origin
}
/api/
${
endpoint
}
`
,
{
method
:
'
POST
'
,
body
:
JSON
.
stringify
(
data
),
credentials
:
'
omit
'
,
cache
:
'
no-cache
'
}).
then
(
async
(
res
)
=>
{
const
body
=
res
.
status
===
204
?
null
:
await
res
.
json
();
if
(
res
.
status
===
200
)
{
resolve
(
body
);
}
else
if
(
res
.
status
===
204
)
{
resolve
(
null
);
}
else
{
reject
({
[
MK_API_ERROR
]:
true
,
...
body
.
error
});
}
}).
catch
(
reject
);
});
return
promise
;
}
}
This diff is collapsed.
Click to expand it.
test/api.ts
+
18
−
5
View file @
bdb59253
import
{
reques
t
}
from
'
../src/api
'
;
import
{
APIClien
t
}
from
'
../src/api
'
;
import
{
enableFetchMocks
}
from
'
jest-fetch-mock
'
;
enableFetchMocks
();
...
...
@@ -21,16 +21,22 @@ describe('API', () => {
fetchMock
.
mockResponse
(
async
(
req
)
=>
{
const
body
=
await
req
.
json
();
if
(
req
.
method
==
'
POST
'
&&
req
.
url
==
'
https://misskey.test/api/i
'
)
{
if
(
typeof
body
.
i
!=
'
string
'
)
{
if
(
body
.
i
===
'
TOKEN
'
)
{
return
JSON
.
stringify
({
id
:
'
foo
'
});
}
else
{
return
{
status
:
400
};
}
return
JSON
.
stringify
({
id
:
'
foo
'
});
}
else
{
return
{
status
:
404
};
}
});
const
res
=
await
request
(
'
https://misskey.test
'
,
'
i
'
,
{},
'
TOKEN
'
);
const
cli
=
new
APIClient
({
origin
:
'
https://misskey.test
'
,
credential
:
'
TOKEN
'
,
});
const
res
=
await
cli
.
request
(
'
i
'
);
// validate response
expect
(
res
).
toEqual
({
...
...
@@ -60,11 +66,18 @@ describe('API', () => {
});
try
{
await
request
(
'
https://misskey.test
'
,
'
i
'
,
{},
'
TOKEN
'
);
const
cli
=
new
APIClient
({
origin
:
'
https://misskey.test
'
,
credential
:
'
TOKEN
'
,
});
await
cli
.
request
(
'
i
'
);
}
catch
(
e
)
{
expect
(
e
.
id
).
toEqual
(
'
5d37dbcb-891e-41ca-a3d6-e690c97775ac
'
);
}
});
// TODO: ネットワークエラーのテスト
// TODO: JSON以外が返ってきた場合のハンドリング
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment