Skip to content

Commit

Permalink
style: prettier formatting for v3
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjburton committed Sep 17, 2023
1 parent ba14685 commit 1899032
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 99 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ app.use(
header: "myheader",
maxInterval: 600,
minInterval: 600,
})
}),
);
```

Expand Down Expand Up @@ -229,7 +229,7 @@ const digest = generate(
"POST",
"/api/order",
{ foo: "bar" },
{ order }
{ order },
).digest("hex"); // 76251c6323fbf6355f23816a4c2e12edfd10672517104763ab1b10f078277f86

const hmac = `HMAC ${Date.now().toString()}:${digest}`;
Expand Down Expand Up @@ -268,7 +268,7 @@ const digest = generate(
Date.now().toString(),
"POST",
"/api/order",
{ foo: "bar" }
{ foo: "bar" },
).digest("hex");

const hmac = `HMAC ${Date.now().toString()}:${digest}`;
Expand Down
4 changes: 2 additions & 2 deletions benchmark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ function transformObserverEntry(entry: PerformanceEntry): TransformedEntry {
const observer: PerformanceObserver = new PerformanceObserver(
(items: PerformanceObserverEntryList): void =>
console.log(
items.getEntries().map((entry) => transformObserverEntry(entry))
)
items.getEntries().map((entry) => transformObserverEntry(entry)),
),
);
const middleware = HMAC("secret");
observer.observe({ type: "measure" });
Expand Down
4 changes: 2 additions & 2 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface GenerateOptions {

function transformBody(
body: UnknownObject | unknown[],
order?: Order
order?: Order,
): UnknownObject | unknown[] {
// we never want to order an array as it is deterministic in JSON
if (Array.isArray(body) || typeof order !== "function") {
Expand All @@ -31,7 +31,7 @@ export default function generate(
method: string,
url: string,
body?: UnknownObject | unknown[],
options?: GenerateOptions
options?: GenerateOptions,
): Hmac {
const hmac = createHmac(algorithm, secret);

Expand Down
24 changes: 12 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ export const defaults: Options = {

async function determineSecret(
secret: DynamicSecret,
req: Request
req: Request,
): Promise<string | undefined> {
return typeof secret === "function" ? await secret(req) : secret;
}

export function HMAC(
secret: DynamicSecret,
options: Partial<Options> = {}
options: Partial<Options> = {},
): RequestHandler {
const mergedOpts: Options = { ...defaults, ...options };

Expand All @@ -88,7 +88,7 @@ export function HMAC(
return async function (
request: Request,
_,
next: NextFunction
next: NextFunction,
): Promise<void> {
// we have to create a scoped secret per request, if we were to reassign the original `secret` variable
// the next request that comes in will no longer be the secret function
Expand All @@ -97,17 +97,17 @@ export function HMAC(
if (typeof scopedSecret !== "string" || scopedSecret.length === 0) {
return next(
new AuthError(
`Invalid secret. Expected non-empty string but got '${scopedSecret}' (type: ${typeof scopedSecret})`
)
`Invalid secret. Expected non-empty string but got '${scopedSecret}' (type: ${typeof scopedSecret})`,
),
);
}

const header = request.get(mergedOpts.header);
if (typeof header !== "string") {
return next(
new AuthError(
`Header provided not in sent headers. Expected ${mergedOpts.header} but not found in request.headers`
)
`Header provided not in sent headers. Expected ${mergedOpts.header} but not found in request.headers`,
),
);
}

Expand All @@ -117,8 +117,8 @@ export function HMAC(
) {
return next(
new AuthError(
`Header did not start with correct identifier. Expected ${mergedOpts.identifier} but not found in options.header`
)
`Header did not start with correct identifier. Expected ${mergedOpts.identifier} but not found in options.header`,
),
);
}

Expand All @@ -142,8 +142,8 @@ export function HMAC(
) {
return next(
new AuthError(
"Time difference between generated and requested time is too great"
)
"Time difference between generated and requested time is too great",
),
);
}

Expand All @@ -165,7 +165,7 @@ export function HMAC(
request.method,
request.originalUrl,
request.body,
{ order: mergedOpts.order }
{ order: mergedOpts.order },
).digest();
const sourceDigest = Buffer.from(hashMatch[1], "hex");

Expand Down
2 changes: 1 addition & 1 deletion src/order.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UnknownObject } from "./index.js";

export default function order(
o: UnknownObject | unknown[]
o: UnknownObject | unknown[],
): UnknownObject | unknown[] {
if (typeof o !== "object" || o === null) {
return o;
Expand Down
12 changes: 6 additions & 6 deletions src/validateArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const crypto = await import("node:crypto");
export default function (secret: DynamicSecret, options: Options): void {
if (!secret || (typeof secret !== "string" && typeof secret !== "function")) {
throw new TypeError(
`Invalid value provided for property secret. Expected non-empty string or function but got '${secret}' (type: ${typeof secret})`
`Invalid value provided for property secret. Expected non-empty string or function but got '${secret}' (type: ${typeof secret})`,
);
}

Expand All @@ -16,23 +16,23 @@ export default function (secret: DynamicSecret, options: Options): void {
throw new TypeError(
`Invalid value provided for property options.algorithm. Expected value from crypto.getHashes() but got ${
options.algorithm
} (type: ${typeof options.algorithm})`
} (type: ${typeof options.algorithm})`,
);
}

if (!options.identifier || typeof options.identifier !== "string") {
throw new TypeError(
`Invalid value provided for property options.identifier. Expected non-empty string but got '${
options.identifier
}' (type: ${typeof options.identifier})`
}' (type: ${typeof options.identifier})`,
);
}

if (!options.header || typeof options.header !== "string") {
throw new TypeError(
`Invalid value provided for property options.header. Expected non-empty string but got '${
options.header
}' (type: ${typeof options.header})`
}' (type: ${typeof options.header})`,
);
}

Expand All @@ -44,7 +44,7 @@ export default function (secret: DynamicSecret, options: Options): void {
throw new TypeError(
`Invalid value provided for property options.maxInterval. Expected number but got '${
options.maxInterval
}' (type: ${typeof options.maxInterval})`
}' (type: ${typeof options.maxInterval})`,
);
}

Expand All @@ -56,7 +56,7 @@ export default function (secret: DynamicSecret, options: Options): void {
throw new TypeError(
`Invalid value provided for optional property options.minInterval. Expected positive number but got '${
options.minInterval
}' (type: ${typeof options.minInterval})`
}' (type: ${typeof options.minInterval})`,
);
}
}
20 changes: 10 additions & 10 deletions tests/e2e/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ describe("e2e", () => {
_: Record<string, unknown>,
_req: express.Request,
res: express.Response,
next: express.NextFunction
next: express.NextFunction,
): void => {
res.sendStatus(401);
next();
}
},
);
app.post(
"/test",
(_, res: express.Response): express.Response => res.sendStatus(200)
(_, res: express.Response): express.Response => res.sendStatus(200),
);
connection = app.listen(PORT, done);
});
Expand All @@ -58,7 +58,7 @@ describe("e2e", () => {
time,
options.method,
url.pathname,
body
body,
).digest("hex")}`;
},
],
Expand Down Expand Up @@ -90,19 +90,19 @@ describe("e2e", () => {
_: Record<string, unknown>,
_req: express.Request,
res: express.Response,
next: express.NextFunction
next: express.NextFunction,
): void => {
res.sendStatus(401);
next();
}
},
);
app.post(
"/foo",
(_, res: express.Response): express.Response => res.sendStatus(200)
(_, res: express.Response): express.Response => res.sendStatus(200),
);
app.post(
"/bar",
(_, res: express.Response): express.Response => res.sendStatus(201)
(_, res: express.Response): express.Response => res.sendStatus(201),
);
connection = app.listen(PORT, done);
});
Expand All @@ -129,7 +129,7 @@ describe("e2e", () => {
time,
options.method,
url.pathname,
body
body,
).digest("hex")}`;
},
],
Expand All @@ -156,7 +156,7 @@ describe("e2e", () => {
time,
options.method,
url.pathname,
body
body,
).digest("hex")}`;
},
],
Expand Down
Loading

0 comments on commit 1899032

Please sign in to comment.