Skip to content

Commit

Permalink
Merge pull request #827 from ninoseki/use-command-schema
Browse files Browse the repository at this point in the history
refactor: use CommandSchema/Type
  • Loading branch information
ninoseki authored Jul 28, 2024
2 parents 1f66c39 + 4c654ea commit cf44563
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/command/packer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import truncate from "just-truncate";

import type { Command } from "~/types";
import type { CommandType } from "~/schemas";

const abbreviations = ["ip", "asn", "btc", "cve", "eth", "url"];

function capitalize(s: string) {
return s.charAt(0).toUpperCase() + s.slice(1);
}

export function commandToID(command: Command): string {
export function commandToID(command: CommandType): string {
return JSON.stringify(command);
}

Expand All @@ -24,7 +24,7 @@ function normalizeCommandType(commandType: string): string {
return commandType;
}

export function commandToMessage(command: Command): string {
export function commandToMessage(command: CommandType): string {
return `${capitalize(command.action)} ${truncate(
command.query,
)} as ${normalizeCommandType(command.type)} on ${command.name}`;
Expand Down
7 changes: 3 additions & 4 deletions src/command/runner.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { err, errAsync, ok, okAsync, Result, ResultAsync } from "neverthrow";

import { URLScan } from "~/scanner";
import type { OptionsType } from "~/schemas";
import type { CommandType, OptionsType } from "~/schemas";
import { Selector } from "~/selector";
import type {
Command,
Scanner,
ScannerMap,
Searcher,
Expand All @@ -14,10 +13,10 @@ import type {
import { isScanner, isSearcher } from "~/utils";

export class CommandRunner {
public command: Command;
public command: CommandType;
protected options: OptionsType;

public constructor(command: Command, options: OptionsType) {
public constructor(command: CommandType, options: OptionsType) {
this.command = command;
this.options = options;
}
Expand Down
2 changes: 2 additions & 0 deletions src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ export const CommandSchema = v.object({
type: Searchable,
name: v.string(),
});

export type CommandType = v.InferOutput<typeof CommandSchema>;
9 changes: 1 addition & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Result, ResultAsync } from "neverthrow";

import type { CommandActionType, OptionsType, SearchableType } from "~/schemas";
import type { OptionsType, SearchableType } from "~/schemas";

export const SEARCHABLE_TYPES: SearchableType[] = [
"asn",
Expand Down Expand Up @@ -83,13 +83,6 @@ export interface ScannerMap {
) => ResultAsync<string, string>;
}

export interface Command {
action: CommandActionType;
query: string;
type: SearchableType;
name: string;
}

export const MD5_LENGTH = 32;
export const SHA1_LENGTH = 40;
export const SHA256_LENGTH = 64;

0 comments on commit cf44563

Please sign in to comment.