Skip to content

Commit

Permalink
init request spec management
Browse files Browse the repository at this point in the history
  • Loading branch information
Mazuh committed Jan 31, 2024
1 parent 013a4fb commit e72e34a
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 5 deletions.
26 changes: 21 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@radix-ui/react-slot": "^1.0.2",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"lodash.debounce": "^4.0.8",
"lucide-react": "^0.293.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand All @@ -39,6 +40,7 @@
"@testing-library/react": "^14.1.2",
"@testing-library/user-event": "^14.5.1",
"@types/jest": "^29.5.8",
"@types/lodash.debounce": "^4.0.9",
"@types/react": "^18.2.37",
"@types/react-dom": "^18.2.15",
"@types/uuid": "^9.0.7",
Expand Down
99 changes: 99 additions & 0 deletions src/features/project-workspace/opfs-project-service.ts
Original file line number Diff line number Diff line change
@@ -1 +1,100 @@
/**
* Implements management of each single project using the Origin Private File System.
*/
import { v4 as uuidv4 } from "uuid";
import type {
Project,
ProjectRequestSpec,
ProjectRequestSpecHeader,
} from "@/entities/management";
import { retrieveProject } from "./opfs-project-service";
import { persistProject } from "@/services/opfs-projects-shared-internals";
import debounce from "lodash.debounce";

export { retrieveProject } from "@/services/opfs-projects-shared-internals";

/** Params for `createRequestSpec` */
export interface CreateRequestSpecParams {
projectUuid: Project["uuid"];
}

/**
* Persist a new spec in the project.
*/
export async function createRequestSpec(
params: CreateRequestSpecParams
): Promise<ProjectRequestSpec> {
const creating: ProjectRequestSpec = {
uuid: uuidv4(),
url: "",
method: "GET",
headers: [...DEFAULT_REQUEST_SPEC_HEADERS],
body: "",
};

const project = await retrieveProject(params.projectUuid);
const updatedProject: Project = {
...project,
specs: [...project.specs, creating],
};
await persistProject(updatedProject);

return creating;
}

/** Params for `removeRequestSpec` */
export interface RemoveRequestSpecParams {
projectUuid: Project["uuid"];
removingUuid: ProjectRequestSpec["uuid"];
}

/**
* Remove a spec from the project.
*/
export async function removeRequestSpec(
params: RemoveRequestSpecParams
): Promise<{ specUuid: ProjectRequestSpec["uuid"] }> {
const project = await retrieveProject(params.projectUuid);
const updatedProject: Project = {
...project,
specs: project.specs.filter((spec) => spec.uuid !== params.removingUuid),
};
await persistProject(updatedProject);

return { specUuid: params.removingUuid };
}

/** Params for `patchRequestSpec` */
export interface PatchRequestSpecParams {
projectUuid: Project["uuid"];
patching: Partial<ProjectRequestSpec>;
}

/**
* Update a spec in the project.
*
* Debounced.
*/
export const patchRequestSpec = debounce(doPatchRequestSpec, 300);

async function doPatchRequestSpec(
params: PatchRequestSpecParams
): Promise<ProjectRequestSpec> {
const project = await retrieveProject(params.projectUuid);
const updatedProject: Project = {
...project,
specs: project.specs.map((existing) =>
existing.uuid === params.patching.uuid
? { ...existing, ...params.patching }
: existing
),
};
await persistProject(updatedProject);

return params.patching as ProjectRequestSpec;
}

const DEFAULT_REQUEST_SPEC_HEADERS: readonly ProjectRequestSpecHeader[] = [
{ key: "Content-Type", value: "application/json", isEnabled: false },
{ key: "Accept", value: "application/json", isEnabled: true },
];

0 comments on commit e72e34a

Please sign in to comment.