curl-wrap 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +210 -0
- package/index.js +1217 -0
- package/package.json +28 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Hitesh Khandelwal
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# curl-wrap
|
|
2
|
+
Nodejs library that wraps curl command line
|
|
3
|
+
|
|
4
|
+
## Install
|
|
5
|
+
```sh
|
|
6
|
+
npm install curl-wrap
|
|
7
|
+
# OR
|
|
8
|
+
yarn add curl-wrap
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
const {Curl} = require('curl-wrap');
|
|
15
|
+
|
|
16
|
+
const response = await Curl.get('https://www.google.com');
|
|
17
|
+
console.log(response.body);
|
|
18
|
+
console.log(response.statusCode);
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
hit12369: write documentation for this library
|
|
22
|
+
|
|
23
|
+
Workspace: # curl-wrap
|
|
24
|
+
|
|
25
|
+
Node.js library that wraps the curl command line.
|
|
26
|
+
|
|
27
|
+
## Install
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
npm install curl-wrap
|
|
31
|
+
# OR
|
|
32
|
+
yarn add curl-wrap
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
### Basic Usage
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
const {Curl} = require('curl-wrap');
|
|
41
|
+
|
|
42
|
+
const response = await Curl.get('https://www.example.com');
|
|
43
|
+
console.log(response.body);
|
|
44
|
+
console.log(response.statusCode);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Creating a Curl Instance
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
const response = await Curl.url('https://www.example.com')
|
|
51
|
+
.post()
|
|
52
|
+
.header('Accept', 'application/json')
|
|
53
|
+
.followRedirect(true)
|
|
54
|
+
.timeout(30);
|
|
55
|
+
|
|
56
|
+
// you can also use
|
|
57
|
+
const curl = new Curl();
|
|
58
|
+
curl.url('https://www.example.com');
|
|
59
|
+
curl.post();
|
|
60
|
+
curl.header('Accept', 'application/json');
|
|
61
|
+
curl.followRedirect(true);
|
|
62
|
+
curl.timeout(30);
|
|
63
|
+
const res = await curl;
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Setting Headers
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
curl.header('Content-Type', 'application/json');
|
|
70
|
+
curl.headers({
|
|
71
|
+
'User-Agent': 'curl-wrap/1.0',
|
|
72
|
+
'Accept': 'application/json'
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Setting Cookies
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
curl.cookie('sessionId', 'abc123');
|
|
80
|
+
curl.cookies({
|
|
81
|
+
'sessionId': 'abc123',
|
|
82
|
+
'userId': 'user456'
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// enable in memory cookie storage for all requests
|
|
86
|
+
curl.globalCookies();
|
|
87
|
+
|
|
88
|
+
// use a cookie file to store and read cookies
|
|
89
|
+
curl.cookieFile('cookies.json');
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Setting Proxy
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
curl.proxy('http://proxy.example.com:8080');
|
|
96
|
+
curl.proxy({
|
|
97
|
+
address: 'proxy.example.com',
|
|
98
|
+
port: 8080,
|
|
99
|
+
type: 'http',
|
|
100
|
+
auth: {
|
|
101
|
+
username: 'user',
|
|
102
|
+
password: 'pass'
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Authentication
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
curl.httpAuth('username', 'password');
|
|
111
|
+
curl.bearerToken('your-token');
|
|
112
|
+
curl.apiToken('your-api-token');
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Impersonating a Browser
|
|
116
|
+
|
|
117
|
+
**NOTE**: it will use `curl-impersonate-chrome` and `curl-impersonate-ff` if they are in `PATH`
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
curl.impersonate('chrome');
|
|
121
|
+
curl.impersonate('chromeMobile');
|
|
122
|
+
curl.impersonate('firefox');
|
|
123
|
+
curl.impersonate('safari');
|
|
124
|
+
curl.impersonate('edge');
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Setting Request Method
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
curl.method('POST');
|
|
131
|
+
curl.get();
|
|
132
|
+
curl.post();
|
|
133
|
+
curl.put();
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Setting Request Body
|
|
137
|
+
|
|
138
|
+
```js
|
|
139
|
+
curl.body({key: 'value'});
|
|
140
|
+
curl.body('any data');
|
|
141
|
+
curl.json({key: 'value'});
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Setting POST/PUT Fields
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
curl.field('key', 'value');
|
|
148
|
+
curl.fields({
|
|
149
|
+
key1: value1,
|
|
150
|
+
key2: value2,
|
|
151
|
+
});
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Setting Query Parameters
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
curl.query('search', 'term');
|
|
158
|
+
curl.query({
|
|
159
|
+
search: 'term',
|
|
160
|
+
page: 1
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Setting Timeout
|
|
165
|
+
|
|
166
|
+
```js
|
|
167
|
+
curl.timeout(30); // in seconds
|
|
168
|
+
curl.timeoutMs(30000); // in milliseconds
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Handling Responses
|
|
172
|
+
|
|
173
|
+
```js
|
|
174
|
+
try {
|
|
175
|
+
const response = await curl;
|
|
176
|
+
console.log(response.body);
|
|
177
|
+
console.log(response.statusCode);
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
console.error(error);
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Verbose Output
|
|
185
|
+
|
|
186
|
+
```js
|
|
187
|
+
curl.verbose(true);
|
|
188
|
+
const response = await curl;
|
|
189
|
+
console.log(response.stderr);
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Method Chaining
|
|
193
|
+
|
|
194
|
+
```js
|
|
195
|
+
Curl.url('https://www.example.com')
|
|
196
|
+
.method('GET')
|
|
197
|
+
.header('Accept', 'application/json')
|
|
198
|
+
.followRedirect(true)
|
|
199
|
+
.maxRedirects(5)
|
|
200
|
+
.timeout(30)
|
|
201
|
+
.then(response => {
|
|
202
|
+
console.log(response.body);
|
|
203
|
+
})
|
|
204
|
+
.catch(error => {
|
|
205
|
+
console.error(error);
|
|
206
|
+
})
|
|
207
|
+
.finally(() => {
|
|
208
|
+
console.log('Request completed');
|
|
209
|
+
});
|
|
210
|
+
```
|