Skip to content

Commit

Permalink
chore: move collection types to types.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
zz-hh-aa committed Nov 18, 2024
1 parent 2c04a69 commit f873db3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
28 changes: 1 addition & 27 deletions api.planx.uk/modules/analytics/metabase/collection/controller.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,9 @@
import { z } from "zod";

Check warning on line 1 in api.planx.uk/modules/analytics/metabase/collection/controller.ts

View workflow job for this annotation

GitHub Actions / Run API Tests

'z' is defined but never used. Allowed unused vars must match /^_/u
import type { ValidatedRequestHandler } from "../../../../shared/middleware/validate.js";

Check warning on line 2 in api.planx.uk/modules/analytics/metabase/collection/controller.ts

View workflow job for this annotation

GitHub Actions / Run API Tests

'ValidatedRequestHandler' is defined but never used. Allowed unused vars must match /^_/u
import { checkCollections, newCollection } from "./service.js";
import type { NewCollectionParams } from "./types.js";
import type { NewCollectionRequest } from "./types.js";
import type { Request, Response } from "express";

// Error response type
interface ErrorResponse {
error: string;
}

// Response types
type ApiResponse<T> = {
data?: T;
error?: string;
};

// Define validation schemas
export const newCollectionSchema = z.object({
body: z.object({
name: z.string(),
description: z.string().optional(),
parentId: z.number().optional(),
}),
});

// Define types for validated requests
export type NewCollectionRequest = ValidatedRequestHandler<
typeof newCollectionSchema,
ApiResponse<NewCollectionParams>
>;

// Controller functions
export const checkCollectionsController = async (
_req: Request,
Expand Down
43 changes: 43 additions & 0 deletions api.planx.uk/modules/analytics/metabase/collection/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
import { z } from "zod";
import type { ValidatedRequestHandler } from "../../../../shared/middleware/validate.js";

type ApiResponse<T> = {
data?: T;
error?: string;
};

export const checkCollectionsSchema = z.object({
query: z.object({
name: z.string().min(1, "Name parameter is required"),
}),
});

export type CheckCollectionsRequest = ValidatedRequestHandler<
typeof checkCollectionsSchema,
ApiResponse<CheckCollectionResponse>
>;

export interface CheckCollectionResponse {
collectionId: number | false;
}

export interface NewCollectionParams {
/** The name of the new collection */
name: string;
Expand All @@ -9,3 +32,23 @@ export interface NewCollectionParams {
namespace?: string;
authority_level?: null;
}

export const newCollectionSchema = z.object({
body: z.object({
name: z.string(),
description: z.string().optional(),
parent_id: z.number().optional(),
namespace: z.string().optional(),
authority_level: z.null().optional(),
}),
});

export type NewCollectionRequest = ValidatedRequestHandler<
typeof newCollectionSchema,
ApiResponse<NewCollectionResponse>
>;

export interface NewCollectionResponse {
id: number;
name: string;
}

0 comments on commit f873db3

Please sign in to comment.