Skip to content

Commit 3bf2a57

Browse files
jsdoc for main api methods
1 parent b220afc commit 3bf2a57

File tree

3 files changed

+106
-12
lines changed

3 files changed

+106
-12
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "svelte-api-keys",
33
"description": "API Key Generation, Validation, and Rate Limiting for SvelteKit",
4-
"version": "0.1.2",
4+
"version": "0.1.3",
55
"keywords": [
66
"svelte",
77
"sveltekit",

Diff for: src/lib/api.ts

+76-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,30 @@ export class Api {
1212
private _any: string[] = []
1313
private _all: string[] = []
1414

15+
/**
16+
* Number of seconds per second
17+
*/
1518
public readonly SECOND = 1
16-
public readonly MINUTE = this.SECOND * 60
17-
public readonly HOUR = this.MINUTE * 60
18-
public readonly DAY = this.HOUR * 24
19-
public readonly WEEK = this.DAY * 7
19+
20+
/**
21+
* Number of seconds per minute
22+
*/
23+
public readonly MINUTE = 60
24+
25+
/**
26+
* Number of seconds per hour
27+
*/
28+
public readonly HOUR = 3_600
29+
30+
/**
31+
* Number of seconds per day
32+
*/
33+
public readonly DAY = 86_400
34+
35+
/**
36+
* Number of seconds per week
37+
*/
38+
public readonly WEEK = 604_800
2039

2140
constructor(
2241
private readonly event: RequestEvent,
@@ -25,16 +44,32 @@ export class Api {
2544
public readonly info: KeyInfo | null,
2645
) {}
2746

47+
/**
48+
* Allow anonymous requests without any API key
49+
*/
2850
anonymous() {
2951
this._anon = true
3052
return this
3153
}
3254

55+
/**
56+
* Set the token bucket name to use for rate limiting.
57+
* Each named token bucket maintains a separate rate-limit.
58+
* Without a name, a single rate limit will apply across all calls.
59+
*
60+
* @param {string} name - the name of the token bucket
61+
*/
3362
name(name: string) {
3463
this._name = name
3564
return this
3665
}
3766

67+
/**
68+
* Set the token cost to use when rate-limiting.
69+
* This is the number of tokens consumed when called.
70+
*
71+
* @param {number} cost - the number of tokens to consume
72+
*/
3873
cost(cost: number) {
3974
if (cost < 1) {
4075
error(500, 'API cost must be at least 1')
@@ -43,21 +78,57 @@ export class Api {
4378
return this
4479
}
4580

81+
/**
82+
* Requires that the API Key has the given permission.
83+
*
84+
* @param {string} permission - the permission
85+
*/
4686
has(permission: string) {
4787
this._has = permission
4888
return this
4989
}
5090

91+
/**
92+
* Requires that the API Key has _any_ of the specified permissions.
93+
* This allows you to check for single or all permissions, such as
94+
* `read:my-project` and `read:*` (for all projects)
95+
*
96+
* @param {string[]} permissions - the permissions
97+
*/
5198
any(permissions: string[]) {
5299
this._any = permissions
53100
return this
54101
}
55102

103+
/**
104+
* Requires that the API Key has _all_ of the specified permissions.
105+
* This allows you to check that the caller has permissions on a hierarchy
106+
* of entities.
107+
*
108+
* @param {string[]} permissions - the permissions
109+
*/
56110
all(permissions: string[]) {
57111
this._all = permissions
58112
return this
59113
}
60114

115+
/**
116+
* Verify the call is allowed and is within the rate limit supplied.
117+
* Rate-limiting http headers are added to the response whether the
118+
* request is limited or not.
119+
*
120+
* If the request should be denied an error will be thrown and handled
121+
* by SvelteKit otherwise you can continue with generating the response.
122+
*
123+
* @param {Object} refill - the token bucket refill parameters
124+
* @param {number} refill.rate - the refill rate in tokens per second
125+
* @param {number} refill.size - the size / capacity of the token bucket
126+
* @throws {HttpError} Will throw a 401 error if the request doesn't include an api key and the call is not set to be anonymous
127+
* @throws {HttpError} Will throw a 403 error if the provided api key is invalid
128+
* @throws {HttpError} Will throw a 403 error if the provided api key has expired
129+
* @throws {HttpError} Will throw a 403 error if the provided api key lacks the required permissions
130+
* @throws {HttpError} Will throw a 429 error if the rate-limit is hit
131+
*/
61132
async limit(refill: Refill) {
62133
if (refill.rate <= 0) throw `refill rate must be greater than 0`
63134
if (refill.size < 1) throw `refill size must be at least 1`
@@ -67,7 +138,7 @@ export class Api {
67138

68139
const { key, info } = this
69140

70-
// if not anonymouse, key must be provided
141+
// if not anonymous, key must be provided
71142
if (!this._anon && !key) {
72143
error(401, 'Missing API Key')
73144
}

Diff for: src/lib/refill.ts

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
1-
// seconds per ...
1+
/**
2+
* Number of seconds per second
3+
*/
24
export const SECOND = 1
3-
export const MINUTE = SECOND * 60
4-
export const HOUR = MINUTE * 60
5-
export const DAY = HOUR * 24
6-
export const WEEK = DAY * 7
5+
6+
/**
7+
* Number of seconds per minute
8+
*/
9+
export const MINUTE = 60
10+
11+
/**
12+
* Number of seconds per hour
13+
*/
14+
export const HOUR = 3_600
15+
16+
/**
17+
* Number of seconds per day
18+
*/
19+
export const DAY = 86_400
20+
21+
/**
22+
* Number of seconds per week
23+
*/
24+
export const WEEK = 604_800
725

826
export interface Refill {
927
readonly rate: number
1028
readonly size: number
1129
}
1230

13-
// parse a string representation
31+
/**
32+
* Parses a string representation of a refill parameter
33+
*
34+
* @param value The refill parameter string representation
35+
* @returns A refill object
36+
*/
1437
export function parseRefill(value: string) {
1538
const re = /(?<rate>\d+)\s*\/\s*(?<unit>second|minute|hour|day|week)\s*(,\s*(?<size>\d+))?/gi
1639
const result = re.exec(value)

0 commit comments

Comments
 (0)