Skip to content

Commit 87203a2

Browse files
authored
feat: add TypeScript types (#23)
1 parent fbd1518 commit 87203a2

5 files changed

Lines changed: 127 additions & 6 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ jobs:
1515
uses: artusjs/github-actions/.github/workflows/node-test.yml@v1
1616
with:
1717
os: 'ubuntu-latest, macos-latest'
18-
version: '14, 16, 18'
18+
version: '14, 16, 18, 20'

.github/workflows/release.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
name: Release
22

33
on:
4-
# 合并后自动发布
54
push:
65
branches: [ master ]
76

8-
# 手动发布
97
workflow_dispatch: {}
108

119
jobs:

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
"version": "1.2.0",
44
"description": "A multipart/form-data encoded stream, helper for file upload.",
55
"main": "lib/formstream.js",
6+
"types": "types/formstream.d.ts",
67
"files": [
7-
"lib"
8+
"lib",
9+
"types/formstream.d.ts"
810
],
911
"scripts": {
1012
"test": "egg-bin test",
1113
"cov": "egg-bin cov",
12-
"ci": "npm run lint && npm run cov",
13-
"lint": "jshint ."
14+
"ci": "npm run lint && npm run tsd && npm run cov",
15+
"lint": "jshint .",
16+
"tsd": "tsd"
1417
},
1518
"repository": {
1619
"type": "git",
@@ -31,12 +34,14 @@
3134
"pause-stream": "~0.0.11"
3235
},
3336
"devDependencies": {
37+
"@types/node": "^20.4.3",
3438
"connect-multiparty": "1",
3539
"egg-bin": "^5.6.1",
3640
"express": "^4.16.4",
3741
"jshint": "^2.13.6",
3842
"pedding": "1",
3943
"should": "4",
44+
"tsd": "^0.28.1",
4045
"urllib": "2"
4146
},
4247
"author": "fengmk2 <fengmk2@gmail.com> (https://github.com/fengmk2)",

types/formstream.d.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { Readable, Stream } from 'stream'
2+
3+
declare class FormStream extends Stream {
4+
/**
5+
* Add a normal field to the form.
6+
*
7+
* @param name Name of field
8+
* @param value Value of field
9+
*/
10+
field(name: string, value: string): this
11+
12+
/**
13+
* Add a local file to be uploaded to the form.
14+
*
15+
* @param name Name of file field
16+
* @param filepath Local path of the file to be uploaded
17+
* @param filename Name of the file (will be the base name of filepath if empty)
18+
* @param filesize Size of the file (will not generate Content-Length header if not specified)
19+
*/
20+
file(
21+
name: string,
22+
filepath: string,
23+
filename?: string,
24+
filesize?: number,
25+
): this
26+
27+
/**
28+
* Add a buffer as a file to upload.
29+
*
30+
* @param name Name of field
31+
* @param buffer The buffer to be uploaded
32+
* @param filename The file name that tells the remote server
33+
* @param contentType Content-Type (aka. MIME Type) of content (will be infered with filename if empty)
34+
*/
35+
buffer(
36+
name: string,
37+
buffer: Buffer,
38+
filename: string,
39+
contentType?: string,
40+
): this
41+
42+
/**
43+
* Add a readable stream as a file to upload. Event 'error' will be emitted if an error occured.
44+
*
45+
* @param name Name of field
46+
* @param stream A readable stream to be piped
47+
* @param filename The file name that tells the remote server
48+
* @param contentType Content-Type (aka. MIME Type) of content (will be infered with filename if empty)
49+
* @param size Size of the stream (will not generate Content-Length header if not specified)
50+
*/
51+
stream(
52+
name: string,
53+
stream: Readable,
54+
filename: string,
55+
contentType?: string,
56+
size?: number,
57+
): this
58+
59+
/**
60+
* Get headers for the request.
61+
*
62+
* @param additionalHeaders Additional headers
63+
*/
64+
headers(additionalHeaders?: Record<string, any>): Record<string, any>
65+
}
66+
67+
declare const formStream: {
68+
new (): FormStream
69+
(): FormStream
70+
}
71+
72+
export = formStream

types/formstream.test-d.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import formStream from '..'
2+
import { Readable } from 'stream'
3+
4+
const fs = formStream()
5+
const fsNew = new formStream()
6+
7+
// @ts-expect-error
8+
fs.field('id')
9+
// @ts-expect-error
10+
fs.field('id', 1)
11+
fs.field('id', '1')
12+
13+
// @ts-expect-error
14+
fs.file('avatar')
15+
// @ts-expect-error
16+
fs.file('avatar', 1)
17+
fs.file('avatar', './avatar.png')
18+
fs.file('avatar', './avatar.png', 'avatar.png')
19+
fs.file('avatar', './avatar.png', 'avatar.png', 1000)
20+
21+
// @ts-expect-error
22+
fs.buffer('content')
23+
// @ts-expect-error
24+
fs.buffer('content', Buffer.from('123'))
25+
// @ts-expect-error
26+
fs.buffer('content', '123', '123.txt')
27+
fs.buffer('content', Buffer.from('123'), '123.txt')
28+
fs.buffer('content', Buffer.from('123'), '123.txt', 'text/plain')
29+
30+
// @ts-expect-error
31+
fs.stream('file')
32+
// @ts-expect-error
33+
fs.stream('file', Readable.from('123'))
34+
// @ts-expect-error
35+
fs.stream('file', '123', '123.txt')
36+
fs.stream('file', Readable.from('123'), '123.txt')
37+
fs.stream('file', Readable.from('123'), '123.txt', 'text/plain')
38+
fs.stream('file', Readable.from('123'), '123.txt', 'text/plain', 1000)
39+
40+
fs.headers()
41+
// @ts-expect-error
42+
fs.headers(1)
43+
fs.headers({})
44+
fs.headers({
45+
'x-token': 'xxxx',
46+
})

0 commit comments

Comments
 (0)