@upstash/redis 0.2.1 → 1.0.0-alpha.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/src/client.ts DELETED
@@ -1,626 +0,0 @@
1
- import fetch from 'isomorphic-unfetch';
2
- import {
3
- ClientObjectProps,
4
- MethodReturn,
5
- Part,
6
- ReturnType,
7
- Upstash,
8
- } from './types';
9
-
10
- /**
11
- * Parse Options
12
- */
13
- function parseOptions(
14
- url?: string | ClientObjectProps,
15
- token?: string,
16
- requestOptions: undefined | RequestInit = {}
17
- ): ClientObjectProps {
18
- if (typeof url === 'object' && url !== null) {
19
- return parseOptions(url.url, url.token, url.requestOptions);
20
- }
21
-
22
- // try auto fill from env variables
23
- if (!url && typeof window === 'undefined') {
24
- url = process.env.UPSTASH_REDIS_REST_URL;
25
- token = process.env.UPSTASH_REDIS_REST_TOKEN;
26
- }
27
-
28
- return { url: url as string, token, requestOptions };
29
- }
30
-
31
- /**
32
- * Fetch
33
- */
34
- async function fetchData(
35
- options: ClientObjectProps,
36
- ...parts: Part[]
37
- ): Promise<ReturnType> {
38
- if (!options.url) {
39
- throw 'Database url not found?';
40
- }
41
-
42
- try {
43
- const res = await fetch(options.url!, {
44
- method: 'POST',
45
- body: JSON.stringify(parts),
46
- headers: {
47
- Authorization: `Bearer ${options.token}`,
48
- ...options.requestOptions?.headers,
49
- },
50
- ...options.requestOptions,
51
- });
52
-
53
- const data = await res.json();
54
-
55
- if (!res.ok) {
56
- if (data.error) throw data.error;
57
- throw `Upstash failed with (${res.status}): ${JSON.stringify(
58
- data,
59
- null,
60
- 2
61
- )}`;
62
- }
63
-
64
- return {
65
- data: data.result,
66
- error: null,
67
- };
68
- } catch (err) {
69
- return {
70
- data: null,
71
- // @ts-ignore
72
- error: err,
73
- };
74
- }
75
- }
76
-
77
- /**
78
- * Creates a Upstash Redis instance
79
- *
80
- * @constructor
81
- * @param {Object} options
82
- * @param {string} [options.url]
83
- * @param {string} [options.token]
84
- * @param {Object} [options.requestOptions]
85
- *
86
- * @example
87
- * ```js
88
- * import upstash from '@upstash/redis'
89
- *
90
- * const redis1 = upstash('url', token);
91
- * const redis2 = upstash({ url: '', token: '', requestOptions: {} });
92
- * ```
93
- */
94
-
95
- function upstash(options?: ClientObjectProps): Upstash;
96
- function upstash(url?: string, token?: string): Upstash;
97
- function upstash(url?: string | ClientObjectProps, token?: string): Upstash {
98
- const options: ClientObjectProps = parseOptions(url, token);
99
-
100
- /**
101
- * Auth
102
- */
103
-
104
- function auth(): void {
105
- Object.assign(
106
- options,
107
- {
108
- url: undefined,
109
- token: undefined,
110
- },
111
- parseOptions(arguments[0], arguments[1])
112
- );
113
- }
114
-
115
- /**
116
- * STRING
117
- */
118
-
119
- function append() {
120
- return fetchData(options, 'append', ...arguments);
121
- }
122
- function decr() {
123
- return fetchData(options, 'decr', ...arguments);
124
- }
125
- function decrby() {
126
- return fetchData(options, 'decrby', ...arguments);
127
- }
128
- function get() {
129
- return fetchData(options, 'get', ...arguments);
130
- }
131
- function getrange() {
132
- return fetchData(options, 'getrange', ...arguments);
133
- }
134
- function getset() {
135
- return fetchData(options, 'getset', ...arguments);
136
- }
137
- function incr() {
138
- return fetchData(options, 'incr', ...arguments);
139
- }
140
- function incrby() {
141
- return fetchData(options, 'incrby', ...arguments);
142
- }
143
- function incrbyfloat() {
144
- return fetchData(options, 'incrbyfloat', ...arguments);
145
- }
146
- function mget() {
147
- return fetchData(options, 'mget', ...arguments);
148
- }
149
- function mset() {
150
- return fetchData(options, 'mset', ...arguments);
151
- }
152
- function msetnx() {
153
- return fetchData(options, 'msetnx', ...arguments);
154
- }
155
- function psetex() {
156
- return fetchData(options, 'psetex', ...arguments);
157
- }
158
- function set() {
159
- return fetchData(options, 'set', ...arguments);
160
- }
161
- function setex() {
162
- return fetchData(options, 'setex', ...arguments);
163
- }
164
- function setnx() {
165
- return fetchData(options, 'setnx', ...arguments);
166
- }
167
- function setrange() {
168
- return fetchData(options, 'setrange', ...arguments);
169
- }
170
- function strlen() {
171
- return fetchData(options, 'strlen', ...arguments);
172
- }
173
-
174
- /**
175
- * BITMAPS
176
- */
177
-
178
- function bitcount() {
179
- return fetchData(options, 'bitcount', ...arguments);
180
- }
181
- function bitop() {
182
- return fetchData(options, 'bitop', ...arguments);
183
- }
184
- function bitpos() {
185
- return fetchData(options, 'bitpos', ...arguments);
186
- }
187
- function getbit() {
188
- return fetchData(options, 'getbit', ...arguments);
189
- }
190
- function setbit() {
191
- return fetchData(options, 'setbit', ...arguments);
192
- }
193
-
194
- /**
195
- * CONNECTION
196
- */
197
-
198
- function echo() {
199
- return fetchData(options, 'echo', ...arguments);
200
- }
201
- function ping() {
202
- return fetchData(options, 'ping', ...arguments);
203
- }
204
-
205
- /**
206
- * HASHES
207
- */
208
-
209
- function hdel(): MethodReturn {
210
- return fetchData(options, 'hdel', ...arguments);
211
- }
212
- function hexists(): MethodReturn {
213
- return fetchData(options, 'hexists', ...arguments);
214
- }
215
- function hget(): MethodReturn {
216
- return fetchData(options, 'hget', ...arguments);
217
- }
218
- function hgetall(): MethodReturn {
219
- return fetchData(options, 'hgetall', ...arguments);
220
- }
221
- function hincrby(): MethodReturn {
222
- return fetchData(options, 'hincrby', ...arguments);
223
- }
224
- function hincrbyfloat(): MethodReturn {
225
- return fetchData(options, 'hincrbyfloat', ...arguments);
226
- }
227
- function hkeys(): MethodReturn {
228
- return fetchData(options, 'hkeys', ...arguments);
229
- }
230
- function hlen(): MethodReturn {
231
- return fetchData(options, 'hlen', ...arguments);
232
- }
233
- function hmget(): MethodReturn {
234
- return fetchData(options, 'hmget', ...arguments);
235
- }
236
- function hmset(): MethodReturn {
237
- return fetchData(options, 'hmset', ...arguments);
238
- }
239
- function hscan(): MethodReturn {
240
- return fetchData(options, 'hscan', ...arguments);
241
- }
242
- function hset(): MethodReturn {
243
- return fetchData(options, 'hset', ...arguments);
244
- }
245
- function hsetnx(): MethodReturn {
246
- return fetchData(options, 'hsetnx', ...arguments);
247
- }
248
- function hvals(): MethodReturn {
249
- return fetchData(options, 'hvals', ...arguments);
250
- }
251
-
252
- /**
253
- * KEYS
254
- */
255
-
256
- function del(): MethodReturn {
257
- return fetchData(options, 'del', ...arguments);
258
- }
259
- function exists(): MethodReturn {
260
- return fetchData(options, 'exists', ...arguments);
261
- }
262
- function expire(): MethodReturn {
263
- return fetchData(options, 'expire', ...arguments);
264
- }
265
- function expireat(): MethodReturn {
266
- return fetchData(options, 'expireat', ...arguments);
267
- }
268
- function keys(): MethodReturn {
269
- return fetchData(options, 'keys', ...arguments);
270
- }
271
- function persist(): MethodReturn {
272
- return fetchData(options, 'persist', ...arguments);
273
- }
274
- function pexpire(): MethodReturn {
275
- return fetchData(options, 'pexpire', ...arguments);
276
- }
277
- function pexpireat(): MethodReturn {
278
- return fetchData(options, 'pexpireat', ...arguments);
279
- }
280
- function pttl(): MethodReturn {
281
- return fetchData(options, 'pttl', ...arguments);
282
- }
283
- function randomkey(): MethodReturn {
284
- return fetchData(options, 'randomkey', ...arguments);
285
- }
286
- function rename(): MethodReturn {
287
- return fetchData(options, 'rename', ...arguments);
288
- }
289
- function renamenx(): MethodReturn {
290
- return fetchData(options, 'renamenx', ...arguments);
291
- }
292
- function scan(): MethodReturn {
293
- return fetchData(options, 'scan', ...arguments);
294
- }
295
- function touch(): MethodReturn {
296
- return fetchData(options, 'touch', ...arguments);
297
- }
298
- function ttl(): MethodReturn {
299
- return fetchData(options, 'ttl', ...arguments);
300
- }
301
- function type(): MethodReturn {
302
- return fetchData(options, 'type', ...arguments);
303
- }
304
- function unlink(): MethodReturn {
305
- return fetchData(options, 'unlink', ...arguments);
306
- }
307
-
308
- /**
309
- * LISTS
310
- */
311
-
312
- function lindex(): MethodReturn {
313
- return fetchData(options, 'lindex', ...arguments);
314
- }
315
- function linsert(): MethodReturn {
316
- return fetchData(options, 'linsert', ...arguments);
317
- }
318
- function llen(): MethodReturn {
319
- return fetchData(options, 'llen', ...arguments);
320
- }
321
- function lpop(): MethodReturn {
322
- return fetchData(options, 'lpop', ...arguments);
323
- }
324
- function lpush(): MethodReturn {
325
- return fetchData(options, 'lpush', ...arguments);
326
- }
327
- function lpushx(): MethodReturn {
328
- return fetchData(options, 'lpushx', ...arguments);
329
- }
330
- function lrange(): MethodReturn {
331
- return fetchData(options, 'lrange', ...arguments);
332
- }
333
- function lrem(): MethodReturn {
334
- return fetchData(options, 'lrem', ...arguments);
335
- }
336
- function lset(): MethodReturn {
337
- return fetchData(options, 'lset', ...arguments);
338
- }
339
- function ltrim(): MethodReturn {
340
- return fetchData(options, 'ltrim', ...arguments);
341
- }
342
- function rpop(): MethodReturn {
343
- return fetchData(options, 'rpop', ...arguments);
344
- }
345
- function rpoplpush(): MethodReturn {
346
- return fetchData(options, 'rpoplpush', ...arguments);
347
- }
348
- function rpush(): MethodReturn {
349
- return fetchData(options, 'rpush', ...arguments);
350
- }
351
- function rpushx(): MethodReturn {
352
- return fetchData(options, 'rpushx', ...arguments);
353
- }
354
-
355
- /**
356
- * SERVER
357
- */
358
-
359
- function dbsize(): MethodReturn {
360
- return fetchData(options, 'dbsize', ...arguments);
361
- }
362
- function flushall(): MethodReturn {
363
- return fetchData(options, 'flushall', ...arguments);
364
- }
365
- function flushdb(): MethodReturn {
366
- return fetchData(options, 'flushdb', ...arguments);
367
- }
368
- function info(): MethodReturn {
369
- return fetchData(options, 'info', ...arguments);
370
- }
371
- function time(): MethodReturn {
372
- return fetchData(options, 'time', ...arguments);
373
- }
374
-
375
- /**
376
- * SET
377
- */
378
-
379
- function sadd(): MethodReturn {
380
- return fetchData(options, 'sadd', ...arguments);
381
- }
382
- function scard(): MethodReturn {
383
- return fetchData(options, 'scard', ...arguments);
384
- }
385
- function sdiff(): MethodReturn {
386
- return fetchData(options, 'sdiff', ...arguments);
387
- }
388
- function sdiffstore(): MethodReturn {
389
- return fetchData(options, 'sdiffstore', ...arguments);
390
- }
391
- function sinter(): MethodReturn {
392
- return fetchData(options, 'sinter', ...arguments);
393
- }
394
- function sinterstore(): MethodReturn {
395
- return fetchData(options, 'sinterstore', ...arguments);
396
- }
397
- function sismember(): MethodReturn {
398
- return fetchData(options, 'sismember', ...arguments);
399
- }
400
- function smembers(): MethodReturn {
401
- return fetchData(options, 'smembers', ...arguments);
402
- }
403
- function smove(): MethodReturn {
404
- return fetchData(options, 'smove', ...arguments);
405
- }
406
- function spop(): MethodReturn {
407
- return fetchData(options, 'spop', ...arguments);
408
- }
409
- function srandmember(): MethodReturn {
410
- return fetchData(options, 'srandmember', ...arguments);
411
- }
412
- function srem(): MethodReturn {
413
- return fetchData(options, 'srem', ...arguments);
414
- }
415
- function sscan(): MethodReturn {
416
- return fetchData(options, 'sscan', ...arguments);
417
- }
418
- function sunion(): MethodReturn {
419
- return fetchData(options, 'sunion', ...arguments);
420
- }
421
- function sunionstore(): MethodReturn {
422
- return fetchData(options, 'sunionstore', ...arguments);
423
- }
424
-
425
- /**
426
- * SORTED SETS
427
- */
428
-
429
- function zadd(): MethodReturn {
430
- return fetchData(options, 'zadd', ...arguments);
431
- }
432
- function zcard(): MethodReturn {
433
- return fetchData(options, 'zcard', ...arguments);
434
- }
435
- function zcount(): MethodReturn {
436
- return fetchData(options, 'zcount', ...arguments);
437
- }
438
- function zincrby(): MethodReturn {
439
- return fetchData(options, 'zincrby', ...arguments);
440
- }
441
- function zinterstore(): MethodReturn {
442
- return fetchData(options, 'zinterstore', ...arguments);
443
- }
444
- function zlexcount(): MethodReturn {
445
- return fetchData(options, 'zlexcount', ...arguments);
446
- }
447
- function zpopmax(): MethodReturn {
448
- return fetchData(options, 'zpopmax', ...arguments);
449
- }
450
- function zpopmin(): MethodReturn {
451
- return fetchData(options, 'zpopmin', ...arguments);
452
- }
453
- function zrange(): MethodReturn {
454
- return fetchData(options, 'zrange', ...arguments);
455
- }
456
- function zrangebylex(): MethodReturn {
457
- return fetchData(options, 'zrangebylex', ...arguments);
458
- }
459
- function zrangebyscore(): MethodReturn {
460
- return fetchData(options, 'zrangebyscore', ...arguments);
461
- }
462
- function zrank(): MethodReturn {
463
- return fetchData(options, 'zrank', ...arguments);
464
- }
465
- function zrem(): MethodReturn {
466
- return fetchData(options, 'zrem', ...arguments);
467
- }
468
- function zremrangebylex(): MethodReturn {
469
- return fetchData(options, 'zremrangebylex', ...arguments);
470
- }
471
- function zremrangebyrank(): MethodReturn {
472
- return fetchData(options, 'zremrangebyrank', ...arguments);
473
- }
474
- function zremrangebyscore(): MethodReturn {
475
- return fetchData(options, 'zremrangebyscore', ...arguments);
476
- }
477
- function zrevrange(): MethodReturn {
478
- return fetchData(options, 'zrevrange', ...arguments);
479
- }
480
- function zrevrangebylex(): MethodReturn {
481
- return fetchData(options, 'zrevrangebylex', ...arguments);
482
- }
483
- function zrevrangebyscore(): MethodReturn {
484
- return fetchData(options, 'zrevrangebyscore', ...arguments);
485
- }
486
- function zrevrank(): MethodReturn {
487
- return fetchData(options, 'zrevrank', ...arguments);
488
- }
489
- function zscan(): MethodReturn {
490
- return fetchData(options, 'zscan', ...arguments);
491
- }
492
- function zscore(): MethodReturn {
493
- return fetchData(options, 'zscore', ...arguments);
494
- }
495
- function zunionstore(): MethodReturn {
496
- return fetchData(options, 'zunionstore', ...arguments);
497
- }
498
-
499
- return {
500
- auth,
501
- // STRING
502
- append,
503
- decr,
504
- decrby,
505
- get,
506
- getrange,
507
- getset,
508
- incr,
509
- incrby,
510
- incrbyfloat,
511
- mget,
512
- mset,
513
- msetnx,
514
- psetex,
515
- set,
516
- setex,
517
- setnx,
518
- setrange,
519
- strlen,
520
- // BITMAPS
521
- bitcount,
522
- bitop,
523
- bitpos,
524
- getbit,
525
- setbit,
526
- // CONNECTION
527
- echo,
528
- ping,
529
- // HASHES
530
- hdel,
531
- hexists,
532
- hget,
533
- hgetall,
534
- hincrby,
535
- hincrbyfloat,
536
- hkeys,
537
- hlen,
538
- hmget,
539
- hmset,
540
- hscan,
541
- hset,
542
- hsetnx,
543
- hvals,
544
- // KEYS
545
- del,
546
- exists,
547
- expire,
548
- expireat,
549
- keys,
550
- persist,
551
- pexpire,
552
- pexpireat,
553
- pttl,
554
- randomkey,
555
- rename,
556
- renamenx,
557
- scan,
558
- touch,
559
- ttl,
560
- type,
561
- unlink,
562
- // LISTS
563
- lindex,
564
- linsert,
565
- llen,
566
- lpop,
567
- lpush,
568
- lpushx,
569
- lrange,
570
- lrem,
571
- lset,
572
- ltrim,
573
- rpop,
574
- rpoplpush,
575
- rpush,
576
- rpushx,
577
- // SERVER
578
- dbsize,
579
- flushall,
580
- flushdb,
581
- info,
582
- time,
583
- // SET
584
- sadd,
585
- scard,
586
- sdiff,
587
- sdiffstore,
588
- sinter,
589
- sinterstore,
590
- sismember,
591
- smembers,
592
- smove,
593
- spop,
594
- srandmember,
595
- srem,
596
- sscan,
597
- sunion,
598
- sunionstore,
599
- // SORTED SETS
600
- zadd,
601
- zcard,
602
- zcount,
603
- zincrby,
604
- zinterstore,
605
- zlexcount,
606
- zpopmax,
607
- zpopmin,
608
- zrange,
609
- zrangebylex,
610
- zrangebyscore,
611
- zrank,
612
- zrem,
613
- zremrangebylex,
614
- zremrangebyrank,
615
- zremrangebyscore,
616
- zrevrange,
617
- zrevrangebylex,
618
- zrevrangebyscore,
619
- zrevrank,
620
- zscan,
621
- zscore,
622
- zunionstore,
623
- };
624
- }
625
-
626
- export default upstash;