CREDENTIAL TYPE MANAGEMENT WORKS
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { credentialTypes } from "../../managers/credentialTypes";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
import { z } from "zod";
|
||||
|
||||
/* /v1/credential-type */
|
||||
export default createRoute(
|
||||
"post",
|
||||
["/"],
|
||||
|
||||
async (c) => {
|
||||
const body = await c.req.json();
|
||||
|
||||
const schema = z.object({
|
||||
name: z.string().min(1, "Name is required"),
|
||||
permissionScope: z.string().min(1, "Permission scope is required"),
|
||||
icon: z.string().optional(),
|
||||
fields: z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
required: z.boolean(),
|
||||
secure: z.boolean(),
|
||||
valueType: z.enum(["plain_text", "password"]),
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
const data = schema.parse(body);
|
||||
|
||||
const credentialType = await credentialTypes.create(data as any);
|
||||
|
||||
const response = apiResponse.created(
|
||||
"Credential Type Created Successfully!",
|
||||
credentialType.toJson(),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["credential_type.create"] }),
|
||||
);
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { credentialTypes } from "../../managers/credentialTypes";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
|
||||
/* /v1/credential-type/:id */
|
||||
export default createRoute(
|
||||
"delete",
|
||||
["/:id"],
|
||||
|
||||
async (c) => {
|
||||
await credentialTypes.delete(c.req.param("id"));
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Credential Type Deleted Successfully!",
|
||||
null,
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["credential_type.delete"] }),
|
||||
);
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { credentialTypes } from "../../managers/credentialTypes";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
|
||||
/* /v1/credential-type/:identifier */
|
||||
export default createRoute(
|
||||
"get",
|
||||
["/:identifier"],
|
||||
|
||||
async (c) => {
|
||||
const credentialType = await credentialTypes.fetch(
|
||||
c.req.param("identifier"),
|
||||
);
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Credential Type Fetched Successfully!",
|
||||
credentialType.toJson({ includeCredentialCount: true }),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["credential_type.fetch"] }),
|
||||
);
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { credentialTypes } from "../../managers/credentialTypes";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
|
||||
/* /v1/credential-type */
|
||||
export default createRoute(
|
||||
"get",
|
||||
["/"],
|
||||
|
||||
async (c) => {
|
||||
const allCredentialTypes = await credentialTypes.fetchAll();
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Credential Types Fetched Successfully!",
|
||||
allCredentialTypes.map((ct) =>
|
||||
ct.toJson({ includeCredentialCount: true }),
|
||||
),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["credential_type.fetch.many"] }),
|
||||
);
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { credentialTypes } from "../../managers/credentialTypes";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
|
||||
/* /v1/credential-type/:id/credentials */
|
||||
export default createRoute(
|
||||
"get",
|
||||
["/:id/credentials"],
|
||||
|
||||
async (c) => {
|
||||
const credentialType = await credentialTypes.fetch(c.req.param("id"));
|
||||
const credentials = await credentialType.fetchCredentials();
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Credentials Fetched Successfully!",
|
||||
credentials.map((cred) => cred.toJson()),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({
|
||||
permissions: ["credential_type.fetch", "credential.fetch.many"],
|
||||
}),
|
||||
);
|
||||
@@ -0,0 +1,15 @@
|
||||
import { default as fetch } from "./fetch";
|
||||
import { default as fetchAll } from "./fetchAll";
|
||||
import { default as create } from "./create";
|
||||
import { default as update } from "./update";
|
||||
import { default as deleteCredentialType } from "./delete";
|
||||
import { default as fetchCredentials } from "./fetchCredentials";
|
||||
|
||||
export {
|
||||
fetch,
|
||||
fetchAll,
|
||||
create,
|
||||
update,
|
||||
deleteCredentialType as delete,
|
||||
fetchCredentials,
|
||||
};
|
||||
@@ -0,0 +1,46 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { credentialTypes } from "../../managers/credentialTypes";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
import { z } from "zod";
|
||||
|
||||
/* /v1/credential-type/:id */
|
||||
export default createRoute(
|
||||
"patch",
|
||||
["/:id"],
|
||||
|
||||
async (c) => {
|
||||
const body = await c.req.json();
|
||||
const credentialType = await credentialTypes.fetch(c.req.param("id"));
|
||||
|
||||
const schema = z.object({
|
||||
name: z.string().optional(),
|
||||
permissionScope: z.string().optional(),
|
||||
icon: z.string().optional(),
|
||||
fields: z
|
||||
.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
required: z.boolean(),
|
||||
secure: z.boolean(),
|
||||
valueType: z.enum(["plain_text", "password"]),
|
||||
}),
|
||||
)
|
||||
.optional(),
|
||||
});
|
||||
|
||||
const data = schema.parse(body);
|
||||
|
||||
await credentialType.update(data as any);
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Credential Type Updated Successfully!",
|
||||
credentialType.toJson(),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["credential_type.update"] }),
|
||||
);
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { credentials } from "../../managers/credentials";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
import { z } from "zod";
|
||||
|
||||
/* /v1/credential/create */
|
||||
export default createRoute(
|
||||
"post",
|
||||
["/credentials"],
|
||||
|
||||
async (c) => {
|
||||
const body = await c.req.json();
|
||||
|
||||
const schema = z.object({
|
||||
name: z.string().min(1, "Name is required"),
|
||||
typeId: z.string().min(1, "Type ID is required"),
|
||||
companyId: z.string().min(1, "Company ID is required"),
|
||||
fields: z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
fieldId: z.string(),
|
||||
value: z.string(),
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
const data = schema.parse(body);
|
||||
|
||||
const credential = await credentials.create(data);
|
||||
|
||||
const response = apiResponse.created(
|
||||
"Credential Created Successfully!",
|
||||
credential.toJson(),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["credential.create"] }),
|
||||
);
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { credentials } from "../../managers/credentials";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
|
||||
/* /v1/credential/:id */
|
||||
export default createRoute(
|
||||
"delete",
|
||||
["/credentials/:id"],
|
||||
|
||||
async (c) => {
|
||||
await credentials.delete(c.req.param("id"));
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Credential Deleted Successfully!",
|
||||
null,
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["credential.delete"] }),
|
||||
);
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { credentials } from "../../managers/credentials";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
|
||||
/* /v1/credential/:id */
|
||||
export default createRoute(
|
||||
"get",
|
||||
["/credentials/:id"],
|
||||
|
||||
async (c) => {
|
||||
const credential = await credentials.fetch(c.req.param("id"));
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Credential Fetched Successfully!",
|
||||
credential.toJson(),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["credential.fetch"] }),
|
||||
);
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { credentials } from "../../managers/credentials";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
|
||||
/* /v1/credential/company/:companyId */
|
||||
export default createRoute(
|
||||
"get",
|
||||
["/credentials/company/:companyId"],
|
||||
|
||||
async (c) => {
|
||||
const companyCredentials = await credentials.fetchByCompany(
|
||||
c.req.param("companyId"),
|
||||
);
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Company Credentials Fetched Successfully!",
|
||||
companyCredentials.map((cred) => cred.toJson()),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["credential.fetch.many"] }),
|
||||
);
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { credentials } from "../../managers/credentials";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
|
||||
/* /v1/credential/:id/fields */
|
||||
export default createRoute(
|
||||
"get",
|
||||
["/credentials/:id/fields"],
|
||||
|
||||
async (c) => {
|
||||
const credential = await credentials.fetch(c.req.param("id"));
|
||||
const fields = await credential.fetchAllFieldValues();
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Credential Fields Fetched Successfully!",
|
||||
fields,
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({
|
||||
permissions: ["credential.fetch", "credential.fields.fetch"],
|
||||
}),
|
||||
);
|
||||
@@ -0,0 +1,19 @@
|
||||
import { default as fetch } from "./fetch";
|
||||
import { default as fetchByCompany } from "./fetchByCompany";
|
||||
import { default as create } from "./create";
|
||||
import { default as update } from "./update";
|
||||
import { default as updateFields } from "./updateFields";
|
||||
import { default as fetchFields } from "./fetchFields";
|
||||
import { default as readSecureValues } from "./readSecureValues";
|
||||
import { default as deleteCredential } from "./delete";
|
||||
|
||||
export {
|
||||
fetch,
|
||||
fetchByCompany,
|
||||
create,
|
||||
update,
|
||||
updateFields,
|
||||
fetchFields,
|
||||
readSecureValues,
|
||||
deleteCredential as delete,
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { credentials } from "../../managers/credentials";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
|
||||
/* /v1/credential/:id/secure-values */
|
||||
export default createRoute(
|
||||
"get",
|
||||
["/credentials/:id/secure-values"],
|
||||
|
||||
async (c) => {
|
||||
const credential = await credentials.fetch(c.req.param("id"));
|
||||
const secureValues = await credential.readAllSecureValues();
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Secure Values Fetched Successfully!",
|
||||
secureValues,
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({
|
||||
permissions: ["credential.fetch", "credential.secure_values.read"],
|
||||
}),
|
||||
);
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { credentials } from "../../managers/credentials";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
import { z } from "zod";
|
||||
|
||||
/* /v1/credential/:id */
|
||||
export default createRoute(
|
||||
"patch",
|
||||
["/credentials/:id"],
|
||||
|
||||
async (c) => {
|
||||
const body = await c.req.json();
|
||||
const credential = await credentials.fetch(c.req.param("id"));
|
||||
|
||||
const schema = z.object({
|
||||
name: z.string().optional(),
|
||||
});
|
||||
|
||||
const data = schema.parse(body);
|
||||
|
||||
await credential.update(data);
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Credential Updated Successfully!",
|
||||
credential.toJson(),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["credential.update"] }),
|
||||
);
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { credentials } from "../../managers/credentials";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
import { z } from "zod";
|
||||
|
||||
/* /v1/credential/:id/fields */
|
||||
export default createRoute(
|
||||
"put",
|
||||
["/credentials/:id/fields"],
|
||||
|
||||
async (c) => {
|
||||
const body = await c.req.json();
|
||||
const credential = await credentials.fetch(c.req.param("id"));
|
||||
|
||||
const schema = z.object({
|
||||
fields: z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
fieldId: z.string(),
|
||||
value: z.string(),
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
const data = schema.parse(body);
|
||||
|
||||
await credential.validateAndUpdateFields(data.fields);
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Credential Fields Updated Successfully!",
|
||||
credential.toJson(),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({
|
||||
permissions: ["credential.update", "credential.fields.update"],
|
||||
}),
|
||||
);
|
||||
@@ -0,0 +1,7 @@
|
||||
import { Hono } from "hono";
|
||||
import * as credentialRoutes from "../credentials";
|
||||
|
||||
const credentialRouter = new Hono();
|
||||
Object.values(credentialRoutes).map((r) => credentialRouter.route("/", r));
|
||||
|
||||
export default credentialRouter;
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Hono } from "hono";
|
||||
import * as credentialTypeRoutes from "../credential-types";
|
||||
|
||||
const credentialTypeRouter = new Hono();
|
||||
Object.values(credentialTypeRoutes).map((r) =>
|
||||
credentialTypeRouter.route("/", r),
|
||||
);
|
||||
|
||||
export default credentialTypeRouter;
|
||||
@@ -50,6 +50,8 @@ v1.route("/teapot", teapot);
|
||||
v1.route("/auth", require("./routers/authRouter").default);
|
||||
v1.route("/user", require("./routers/user").default);
|
||||
v1.route("/company", require("./routers/companyRouter").default);
|
||||
v1.route("/credential", require("./routers/credentialRouter").default);
|
||||
v1.route("/credential-type", require("./routers/credentialTypeRouter").default);
|
||||
app.route("/v1", v1);
|
||||
|
||||
export default app;
|
||||
|
||||
Reference in New Issue
Block a user