Skip to content

Commit 868a676

Browse files
Merge pull request #19 from deepgram/temp-keys
Added the ability to create temporary keys
2 parents 50472ee + 4aa56e6 commit 868a676

File tree

5 files changed

+110
-3
lines changed

5 files changed

+110
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Updated
11+
12+
- Updated the `keys.create` function to allow new `expirationDate` or `timeToLive`
13+
values. These are optional and one at most can be provided. Providing both will
14+
throw an error.
15+
1016
---
1117

1218
## [1.1.0]

src/keys.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { _request } from "./httpRequest";
2-
import { KeyResponse, Key } from "./types";
2+
import { CreateKeyOptions, KeyResponse, Key } from "./types";
33

44
export class Keys {
55
constructor(private _credentials: string, private _apiUrl: string) {}
@@ -38,18 +38,40 @@ export class Keys {
3838
* @param projectId Unique identifier of the project to create an API key under
3939
* @param comment Comment to describe the key
4040
* @param scopes Permission scopes associated with the API key
41+
* @param options Optional options used when creating API keys
4142
*/
4243
async create(
4344
projectId: string,
4445
comment: string,
45-
scopes: Array<string>
46+
scopes: Array<string>,
47+
options?: CreateKeyOptions
4648
): Promise<Key> {
49+
/** Throw an error if the user provided both expirationDate and timeToLive */
50+
if (
51+
options &&
52+
options.expirationDate !== undefined &&
53+
options.timeToLive !== undefined
54+
) {
55+
throw new Error(
56+
"Please provide expirationDate or timeToLive or neither. Providing both is not allowed."
57+
);
58+
}
59+
4760
return _request<Key>(
4861
"POST",
4962
this._credentials,
5063
this._apiUrl,
5164
`${this.apiPath}/${projectId}/keys`,
52-
JSON.stringify({ comment, scopes })
65+
JSON.stringify({
66+
comment,
67+
scopes,
68+
expiration_date:
69+
options && options.expirationDate
70+
? options.expirationDate
71+
: undefined,
72+
time_to_live_in_seconds:
73+
options && options.timeToLive ? options.timeToLive : undefined,
74+
})
5375
);
5476
}
5577

src/types/createKeyOptions.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Optional options used when creating an API key
3+
*/
4+
export type CreateKeyOptions = {
5+
/**
6+
* Date on which the key you would like to create should expire.
7+
*/
8+
expirationDate?: Date;
9+
/**
10+
* Length of time (in seconds) during which the key you would like to create will remain valid.
11+
*/
12+
timeToLive?: number;
13+
};

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./channel";
2+
export * from "./createKeyOptions";
23
export * from "./hit";
34
export * from "./key";
45
export * from "./keyResponse";

tests/keys.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,71 @@ describe("Key tests", () => {
5353
});
5454
});
5555

56+
it("Throws an exception if both expirationDate and timeToLive are provided", function () {
57+
const expectedError = `Please provide expirationDate or timeToLive or neither. Providing both is not allowed.`;
58+
nock(`https://${fakeUrl}`)
59+
.post(`/v1/projects/${fakeProjectId}/keys`)
60+
.reply(200, mockKey);
61+
62+
keys
63+
.create(fakeProjectId, "test Comment", ["member"], {
64+
expirationDate: new Date(),
65+
timeToLive: 30,
66+
})
67+
.then((response) => {
68+
response.should.deep.eq(mockKey);
69+
requestStub.calledOnce.should.eq(true);
70+
})
71+
.then(() => {
72+
assert.equal(1, 2);
73+
})
74+
.catch((err) => {
75+
assert.equal(err, expectedError);
76+
});
77+
});
78+
79+
it("Does not throw if only timeToLive is provided as an option", function () {
80+
nock(`https://${fakeUrl}`)
81+
.post(`/v1/projects/${fakeProjectId}/keys`)
82+
.reply(200, mockKey);
83+
84+
keys
85+
.create(fakeProjectId, "test Comment", ["member"], {
86+
timeToLive: 30,
87+
})
88+
.then((response) => {
89+
response.should.deep.eq(mockKey);
90+
requestStub.calledOnce.should.eq(true);
91+
})
92+
.then(() => {
93+
assert.equal(1, 1);
94+
})
95+
.catch((err) => {
96+
assert.equal(1, 2);
97+
});
98+
});
99+
100+
it("Does not throw if only expirationDate is provided as an option", function () {
101+
nock(`https://${fakeUrl}`)
102+
.post(`/v1/projects/${fakeProjectId}/keys`)
103+
.reply(200, mockKey);
104+
105+
keys
106+
.create(fakeProjectId, "test Comment", ["member"], {
107+
expirationDate: new Date(),
108+
})
109+
.then((response) => {
110+
response.should.deep.eq(mockKey);
111+
requestStub.calledOnce.should.eq(true);
112+
})
113+
.then(() => {
114+
assert.equal(1, 1);
115+
})
116+
.catch((err) => {
117+
assert.equal(1, 2);
118+
});
119+
});
120+
56121
it("Delete resolves", function () {
57122
nock(`https://${fakeUrl}`)
58123
.delete(`/v1/projects/${fakeProjectId}/keys/${fakeKeyId}`)

0 commit comments

Comments
 (0)