1 import { AdminApiKeyResponse } from "@medusajs/types"
2 import { TFunction } from "i18next"
3 import { ApiKeyType } from "./constants"
4
5 export function getApiKeyTypeFromPathname(pathname: string) {
6 const isSecretKey = pathname.startsWith("/settings/secret-api-keys")
7
8 switch (isSecretKey) {
9 case true:
10 return ApiKeyType.SECRET
11 case false:
12 return ApiKeyType.PUBLISHABLE
13 }
14 }
15
16 export function getApiKeyStatusProps(
17 revokedAt: Date | string | null,
18 t: TFunction
19 ): { color: "red" | "green"; label: string } {
20 if (!revokedAt) {
21 return {
22 color: "green",
23 label: t("apiKeyManagement.status.active"),
24 }
25 }
26
27 return {
28 color: "red",
29 label: t("apiKeyManagement.status.revoked"),
30 }
31 }
32
33 export function getApiKeyTypeProps(
34 type: AdminApiKeyResponse["api_key"]["type"],
35 t: TFunction
36 ): { color: "green" | "blue"; label: string } {
37 if (type === ApiKeyType.PUBLISHABLE) {
38 return {
39 color: "green",
40 label: t("apiKeyManagement.type.publishable"),
41 }
42 }
43
44 return {
45 color: "blue",
46 label: t("apiKeyManagement.type.secret"),
47 }
48 }
49
50 /**
51 * Returns a prettified version of the token with redacted symbols replaced with a bullet point.
52 * @param token - The token to prettify.
53 * @returns The prettified token, with redacted symbols replaced with a bullet point.
54 *
55 * @example
56 * ```ts
57 * const token = "sk_a***yx"
58 * const prettifiedToken = replaceRedactedSymbol(token) // "sk_a•••yx"
59 */
60 export const prettifyRedactedToken = (token: string) => {
61 return token.replace("***", `•••`)
62 }