Skip to content

Commit

Permalink
Merge pull request #19 from deepgram/temp-keys
Browse files Browse the repository at this point in the history
Added the ability to create temporary keys
  • Loading branch information
phazonoverload authored Jan 12, 2022
2 parents 50472ee + 4aa56e6 commit 868a676
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Updated

- Updated the `keys.create` function to allow new `expirationDate` or `timeToLive`
values. These are optional and one at most can be provided. Providing both will
throw an error.

---

## [1.1.0]
Expand Down
28 changes: 25 additions & 3 deletions src/keys.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { _request } from "./httpRequest";
import { KeyResponse, Key } from "./types";
import { CreateKeyOptions, KeyResponse, Key } from "./types";

export class Keys {
constructor(private _credentials: string, private _apiUrl: string) {}
Expand Down Expand Up @@ -38,18 +38,40 @@ export class Keys {
* @param projectId Unique identifier of the project to create an API key under
* @param comment Comment to describe the key
* @param scopes Permission scopes associated with the API key
* @param options Optional options used when creating API keys
*/
async create(
projectId: string,
comment: string,
scopes: Array<string>
scopes: Array<string>,
options?: CreateKeyOptions
): Promise<Key> {
/** Throw an error if the user provided both expirationDate and timeToLive */
if (
options &&
options.expirationDate !== undefined &&
options.timeToLive !== undefined
) {
throw new Error(
"Please provide expirationDate or timeToLive or neither. Providing both is not allowed."
);
}

return _request<Key>(
"POST",
this._credentials,
this._apiUrl,
`${this.apiPath}/${projectId}/keys`,
JSON.stringify({ comment, scopes })
JSON.stringify({
comment,
scopes,
expiration_date:
options && options.expirationDate
? options.expirationDate
: undefined,
time_to_live_in_seconds:
options && options.timeToLive ? options.timeToLive : undefined,
})
);
}

Expand Down
13 changes: 13 additions & 0 deletions src/types/createKeyOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Optional options used when creating an API key
*/
export type CreateKeyOptions = {
/**
* Date on which the key you would like to create should expire.
*/
expirationDate?: Date;
/**
* Length of time (in seconds) during which the key you would like to create will remain valid.
*/
timeToLive?: number;
};
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./channel";
export * from "./createKeyOptions";
export * from "./hit";
export * from "./key";
export * from "./keyResponse";
Expand Down
65 changes: 65 additions & 0 deletions tests/keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,71 @@ describe("Key tests", () => {
});
});

it("Throws an exception if both expirationDate and timeToLive are provided", function () {
const expectedError = `Please provide expirationDate or timeToLive or neither. Providing both is not allowed.`;
nock(`https://${fakeUrl}`)
.post(`/v1/projects/${fakeProjectId}/keys`)
.reply(200, mockKey);

keys
.create(fakeProjectId, "test Comment", ["member"], {
expirationDate: new Date(),
timeToLive: 30,
})
.then((response) => {
response.should.deep.eq(mockKey);
requestStub.calledOnce.should.eq(true);
})
.then(() => {
assert.equal(1, 2);
})
.catch((err) => {
assert.equal(err, expectedError);
});
});

it("Does not throw if only timeToLive is provided as an option", function () {
nock(`https://${fakeUrl}`)
.post(`/v1/projects/${fakeProjectId}/keys`)
.reply(200, mockKey);

keys
.create(fakeProjectId, "test Comment", ["member"], {
timeToLive: 30,
})
.then((response) => {
response.should.deep.eq(mockKey);
requestStub.calledOnce.should.eq(true);
})
.then(() => {
assert.equal(1, 1);
})
.catch((err) => {
assert.equal(1, 2);
});
});

it("Does not throw if only expirationDate is provided as an option", function () {
nock(`https://${fakeUrl}`)
.post(`/v1/projects/${fakeProjectId}/keys`)
.reply(200, mockKey);

keys
.create(fakeProjectId, "test Comment", ["member"], {
expirationDate: new Date(),
})
.then((response) => {
response.should.deep.eq(mockKey);
requestStub.calledOnce.should.eq(true);
})
.then(() => {
assert.equal(1, 1);
})
.catch((err) => {
assert.equal(1, 2);
});
});

it("Delete resolves", function () {
nock(`https://${fakeUrl}`)
.delete(`/v1/projects/${fakeProjectId}/keys/${fakeKeyId}`)
Expand Down

0 comments on commit 868a676

Please sign in to comment.