Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove lodash dependency #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prefixed-api-key",
"version": "1.1.0",
"version": "1.1.1",
"main": "dist/index.js",
"license": "MIT",
"description": "Generate seam-style API-keys",
Expand All @@ -10,7 +10,6 @@
"test": "ava"
},
"devDependencies": {
"@types/lodash": "^4.14.182",
"@types/node": "^17.0.32",
"ava": "^4.2.0",
"esbuild": "^0.14.38",
Expand All @@ -19,7 +18,6 @@
"typescript": "^4.6.4"
},
"dependencies": {
"bs58": "^5.0.0",
"lodash": "^4.17.21"
"bs58": "^5.0.0"
}
}
}
7 changes: 2 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { randomBytes, createHash } from "node:crypto"
import { promisify } from "node:util"
import bs58 from "bs58"
import padStart from "lodash/padStart"

export const hashLongToken = (longToken: string) =>
createHash("sha256").update(longToken).digest("hex")
Expand All @@ -28,14 +27,12 @@ export const generateAPIKey = async ({
generatedRandomBytes(longTokenLength),
])

let shortToken = padStart(
bs58.encode(shortTokenBytes),
let shortToken = bs58.encode(shortTokenBytes).padStart(
shortTokenLength,
"0"
).slice(0, shortTokenLength)

const longToken = padStart(
bs58.encode(longTokenBytes),
const longToken = bs58.encode(longTokenBytes).padStart(
longTokenLength,
"0"
).slice(0, longTokenLength)
Expand Down
13 changes: 13 additions & 0 deletions tests/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,16 @@ test("generate api key should return an empty object when there is no keyPrefix"
t.falsy(apiKey.shortToken)
t.falsy(apiKey.token)
})

test("generate api key should return strings with the correct length", async (t) => {
const shortTokenLength = 10;
const longTokenLength = 20;
const apiKey = await generateAPIKey({
keyPrefix: "mycompany",
shortTokenLength: shortTokenLength,
longTokenLength: longTokenLength,
})

t.truthy(apiKey.longToken && apiKey.longToken.length === longTokenLength)
t.truthy(apiKey.shortToken && apiKey.shortToken.length === shortTokenLength)
})
7 changes: 1 addition & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@types/lodash@^4.14.182":
version "4.14.182"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.182.tgz#05301a4d5e62963227eaafe0ce04dd77c54ea5c2"
integrity sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q==

"@types/node@^17.0.32":
version "17.0.32"
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.32.tgz#51d59d7a90ef2d0ae961791e0900cad2393a0149"
Expand Down Expand Up @@ -894,7 +889,7 @@ locate-path@^7.1.0:
dependencies:
p-locate "^6.0.0"

lodash@^4.17.15, lodash@^4.17.21:
lodash@^4.17.15:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
Expand Down