Skip to content

Commit

Permalink
refactor: change snake case to camel case
Browse files Browse the repository at this point in the history
  • Loading branch information
bxb100 committed Nov 28, 2024
1 parent bad9d5c commit abbc860
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
16 changes: 8 additions & 8 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ describe("ref_regex", () => {
const exec = utils.ref_regex.exec(ref);
expect(exec).not.toBeNull();
expect(exec?.groups).not.toBeNull();
expect(exec?.groups?.vault_name).toBe("vault");
expect(exec?.groups?.item_name).toBe("Secure Note");
expect(exec?.groups?.section_name).toBeUndefined();
expect(exec?.groups?.field_name).toBe("field");
expect(exec?.groups?.vaultName).toBe("vault");
expect(exec?.groups?.itemName).toBe("Secure Note");
expect(exec?.groups?.sectionName).toBeUndefined();
expect(exec?.groups?.fieldName).toBe("field");
});

it("with section", () => {
Expand All @@ -183,10 +183,10 @@ describe("ref_regex", () => {
const exec = utils.ref_regex.exec(ref);
expect(exec).not.toBeNull();
expect(exec?.groups).not.toBeNull();
expect(exec?.groups?.vault_name).toBe("vault");
expect(exec?.groups?.item_name).toBe("item");
expect(exec?.groups?.section_name).toBe("section");
expect(exec?.groups?.field_name).toBe("text");
expect(exec?.groups?.vaultName).toBe("vault");
expect(exec?.groups?.itemName).toBe("item");
expect(exec?.groups?.sectionName).toBe("section");
expect(exec?.groups?.fieldName).toBe("text");
});
});

Expand Down
36 changes: 18 additions & 18 deletions src/auth/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,41 @@ export class Connect implements SecretReferenceResolver {
if (!match) {
throw new Error(`Invalid secret reference: ${path}`);
}
const { vault_name, item_name, section_name, field_name } =
const { vaultName, itemName, sectionName, fieldName } =
match.groups as SecretReference;

const vault = await this.op.getVault(vault_name);
const vault = await this.op.getVault(vaultName);
if (!vault.id) {
return undefined;
}
const item = await this.op.getItem(vault.id, item_name);
const item = await this.op.getItem(vault.id, itemName);

let item_fields = item.fields;
const err_filed_name = section_name ? `${section_name}.${field_name}` : field_name;
if (section_name) {
let itemFields = item.fields;
const errFiledName = sectionName ? `${sectionName}.${fieldName}` : fieldName;
if (sectionName) {
const section = item.sections?.filter(
(s) => s.label === section_name || s.id === section_name,
(s) => s.label === sectionName || s.id === sectionName,
);
if (section === undefined || section.length == 0) {
throw new Error(`The item does not have a field '${err_filed_name}'`)
throw new Error(`The item does not have a field '${errFiledName}'`)
}
const section_ids= section.map(s => s.id!);
item_fields = item_fields?.filter(
(f) => f.section && section_ids.includes(f.section.id!)
const sectionIds= section.map(s => s.id!);
itemFields = itemFields?.filter(
(f) => f.section && sectionIds.includes(f.section.id!)
);
}

const matched_fields = item_fields?.filter(
(f) => f.id === field_name || f.label === field_name,
const matchedFields = itemFields?.filter(
(f) => f.id === fieldName || f.label === fieldName,
);

if (matched_fields == undefined || matched_fields.length == 0) {
throw new Error(`The item does not have a field '${err_filed_name}'`);
if (matchedFields == undefined || matchedFields.length == 0) {
throw new Error(`The item does not have a field '${errFiledName}'`);
}
if (matched_fields.length > 1) {
throw new Error(`The item has more than one '${err_filed_name}' field`);
if (matchedFields.length > 1) {
throw new Error(`The item has more than one '${errFiledName}' field`);
}

return matched_fields[0]!.value;
return matchedFields[0]!.value;
}
}
8 changes: 4 additions & 4 deletions src/auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ export interface SecretReferenceResolver {
}

export type SecretReference = {
vault_name: string;
item_name: string;
section_name: string | null;
field_name: string;
vaultName: string;
itemName: string;
sectionName: string | null;
fieldName: string;
};
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import process from "node:process";
* see more <https://developer.1password.com/docs/cli/secret-references/>
*/
export const ref_regex =
/^op:\/\/(?<vault_name>[^/]+)\/(?<item_name>[^/]+)\/((?<section_name>[^/]+)\/)?(?<field_name>[^/]+)$/;
/^op:\/\/(?<vaultName>[^/]+)\/(?<itemName>[^/]+)\/((?<sectionName>[^/]+)\/)?(?<fieldName>[^/]+)$/;

export const getAuth = (): SecretReferenceResolver => {
const isConnect = process.env[envConnectHost] && process.env[envConnectToken];
Expand Down

0 comments on commit abbc860

Please sign in to comment.