domain apis (#146)

* fix: create domain api

* add domain apis

* fix url

---------

Co-authored-by: harshsbhat <harsh121102@gmail.com>
This commit is contained in:
KM Koushik
2025-04-05 06:48:03 +11:00
committed by GitHub
parent 43600419cb
commit 70026cb11d
11 changed files with 415 additions and 12 deletions

View File

@@ -0,0 +1,3 @@
---
openapi: post /v1/domains
---

View File

@@ -0,0 +1,3 @@
---
openapi: put /v1/domains/{id}/verify
---

View File

@@ -85,6 +85,22 @@
}, },
"updatedAt": { "updatedAt": {
"type": "string" "type": "string"
},
"dmarcAdded": {
"type": "boolean",
"default": false
},
"isVerifying": {
"type": "boolean",
"default": false
},
"errorMessage": {
"type": "string",
"nullable": true
},
"subdomain": {
"type": "string",
"nullable": true
} }
}, },
"required": [ "required": [
@@ -102,6 +118,158 @@
} }
} }
} }
},
"post": {
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"region": {
"type": "string"
}
},
"required": [
"name",
"region"
]
}
}
}
},
"responses": {
"200": {
"description": "Create a new domain",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"id": {
"type": "number",
"description": "The ID of the domain",
"example": 1
},
"name": {
"type": "string",
"description": "The name of the domain",
"example": "example.com"
},
"teamId": {
"type": "number",
"description": "The ID of the team",
"example": 1
},
"status": {
"type": "string",
"enum": [
"NOT_STARTED",
"PENDING",
"SUCCESS",
"FAILED",
"TEMPORARY_FAILURE"
]
},
"region": {
"type": "string",
"default": "us-east-1"
},
"clickTracking": {
"type": "boolean",
"default": false
},
"openTracking": {
"type": "boolean",
"default": false
},
"publicKey": {
"type": "string"
},
"dkimStatus": {
"type": "string",
"nullable": true
},
"spfDetails": {
"type": "string",
"nullable": true
},
"createdAt": {
"type": "string"
},
"updatedAt": {
"type": "string"
},
"dmarcAdded": {
"type": "boolean",
"default": false
},
"isVerifying": {
"type": "boolean",
"default": false
},
"errorMessage": {
"type": "string",
"nullable": true
},
"subdomain": {
"type": "string",
"nullable": true
}
},
"required": [
"id",
"name",
"teamId",
"status",
"publicKey",
"createdAt",
"updatedAt"
]
}
}
}
}
}
}
},
"/v1/domains/{id}/verify": {
"put": {
"parameters": [
{
"schema": {
"type": "number",
"example": 1
},
"required": true,
"name": "id",
"in": "path"
}
],
"responses": {
"200": {
"description": "Create a new domain",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
},
"required": [
"message"
]
}
}
}
}
}
} }
}, },
"/v1/emails/{emailId}": { "/v1/emails/{emailId}": {

View File

@@ -96,7 +96,9 @@
{ {
"group": "Domains", "group": "Domains",
"pages": [ "pages": [
"api-reference/domains/get-domain" "api-reference/domains/get-domain",
"api-reference/domains/create-domain",
"api-reference/domains/verify-domain"
] ]
} }
], ],

View File

@@ -18,14 +18,8 @@ export const DomainSchema = z.object({
spfDetails: z.string().optional().nullish(), spfDetails: z.string().optional().nullish(),
createdAt: z.string(), createdAt: z.string(),
updatedAt: z.string(), updatedAt: z.string(),
}); dmarcAdded: z.boolean().default(false),
isVerifying: z.boolean().default(false),
export const CreateDomainSchema = DomainSchema.omit({ errorMessage: z.string().optional().nullish(),
id: true, subdomain: z.string().optional().nullish(),
createdAt: true,
updatedAt: true,
teamId: true,
publicKey: true,
dkimStatus: true,
spfDetails: true,
}); });

View File

@@ -0,0 +1,45 @@
import { createRoute, z } from "@hono/zod-openapi";
import { DomainSchema } from "~/lib/zod/domain-schema";
import { PublicAPIApp } from "~/server/public-api/hono";
import { createDomain as createDomainService } from "~/server/service/domain-service";
import { getTeamFromToken } from "~/server/public-api/auth";
const route = createRoute({
method: "post",
path: "/v1/domains",
request: {
body: {
required: true,
content: {
"application/json": {
schema: z.object({
name: z.string(),
region: z.string(),
}),
},
},
},
},
responses: {
200: {
content: {
"application/json": {
schema: DomainSchema,
},
},
description: "Create a new domain",
},
},
});
function createDomain(app: PublicAPIApp) {
app.openapi(route, async (c) => {
const team = await getTeamFromToken(c);
const body = c.req.valid("json");
const response = await createDomainService(team.id, body.name, body.region);
return c.json(response);
});
}
export default createDomain;

View File

@@ -0,0 +1,49 @@
import { createRoute, z } from "@hono/zod-openapi";
import { PublicAPIApp } from "~/server/public-api/hono";
import { getTeamFromToken } from "~/server/public-api/auth";
import { db } from "~/server/db";
const route = createRoute({
method: "put",
path: "/v1/domains/{id}/verify",
request: {
params: z.object({
id: z.coerce.number().openapi({
param: {
name: "id",
in: "path",
},
example: 1,
}),
}),
},
responses: {
200: {
content: {
"application/json": {
schema: z.object({
message: z.string(),
}),
},
},
description: "Create a new domain",
},
},
});
function verifyDomain(app: PublicAPIApp) {
app.openapi(route, async (c) => {
const team = await getTeamFromToken(c);
await db.domain.update({
where: { id: c.req.valid("param").id },
data: { isVerifying: true },
});
return c.json({
message: "Domain verification started",
});
});
}
export default verifyDomain;

View File

@@ -9,12 +9,16 @@ import updateEmailScheduledAt from "./api/emails/update-email";
import cancelScheduledEmail from "./api/emails/cancel-email"; import cancelScheduledEmail from "./api/emails/cancel-email";
import getContacts from "./api/contacts/get-contacts"; import getContacts from "./api/contacts/get-contacts";
import upsertContact from "./api/contacts/upsert-contact"; import upsertContact from "./api/contacts/upsert-contact";
import createDomain from "./api/domains/create-domain";
import deleteContact from "./api/contacts/delete-contact"; import deleteContact from "./api/contacts/delete-contact";
import verifyDomain from "./api/domains/verify-domain";
export const app = getApp(); export const app = getApp();
/**Domain related APIs */ /**Domain related APIs */
getDomains(app); getDomains(app);
createDomain(app);
verifyDomain(app);
/**Email related APIs */ /**Email related APIs */
getEmail(app); getEmail(app);

View File

@@ -1,6 +1,6 @@
{ {
"name": "unsend", "name": "unsend",
"version": "1.4.0", "version": "1.4.1",
"description": "", "description": "",
"main": "./dist/index.js", "main": "./dist/index.js",
"module": "./dist/index.mjs", "module": "./dist/index.mjs",

View File

@@ -0,0 +1,57 @@
import { paths } from "../types/schema";
import { ErrorResponse } from "../types";
import { Unsend } from "./unsend";
type CreateDomainPayload =
paths["/v1/domains"]["post"]["requestBody"]["content"]["application/json"];
type CreateDomainResponse = {
data: CreateDomainResponseSuccess | null;
error: ErrorResponse | null;
};
type CreateDomainResponseSuccess =
paths["/v1/domains"]["post"]["responses"]["200"]["content"]["application/json"];
type GetDomainsResponse = {
data: GetDomainsResponseSuccess | null;
error: ErrorResponse | null;
};
type GetDomainsResponseSuccess =
paths["/v1/domains"]["get"]["responses"]["200"]["content"]["application/json"];
type VerifyDomainResponse = {
data: VerifyDomainResponseSuccess | null;
error: ErrorResponse | null;
};
type VerifyDomainResponseSuccess =
paths["/v1/domains/{id}/verify"]["put"]["responses"]["200"]["content"]["application/json"];
export class Domains {
constructor(private readonly unsend: Unsend) {
this.unsend = unsend;
}
async list(): Promise<GetDomainsResponse> {
const data = await this.unsend.get<GetDomainsResponseSuccess>("/domains");
return data;
}
async create(payload: CreateDomainPayload): Promise<CreateDomainResponse> {
const data = await this.unsend.post<CreateDomainResponseSuccess>(
"/domains",
payload
);
return data;
}
async verify(id: number): Promise<VerifyDomainResponse> {
const data = await this.unsend.put<VerifyDomainResponseSuccess>(
`/domains/${id}/verify`,
{}
);
return data;
}
}

View File

@@ -40,11 +40,89 @@ export interface paths {
spfDetails?: string | null; spfDetails?: string | null;
createdAt: string; createdAt: string;
updatedAt: string; updatedAt: string;
/** @default false */
dmarcAdded?: boolean;
/** @default false */
isVerifying?: boolean;
errorMessage?: string | null;
subdomain?: string | null;
})[]; })[];
}; };
}; };
}; };
}; };
post: {
requestBody: {
content: {
"application/json": {
name: string;
region: string;
};
};
};
responses: {
/** @description Create a new domain */
200: {
content: {
"application/json": {
/**
* @description The ID of the domain
* @example 1
*/
id: number;
/**
* @description The name of the domain
* @example example.com
*/
name: string;
/**
* @description The ID of the team
* @example 1
*/
teamId: number;
/** @enum {string} */
status: "NOT_STARTED" | "PENDING" | "SUCCESS" | "FAILED" | "TEMPORARY_FAILURE";
/** @default us-east-1 */
region?: string;
/** @default false */
clickTracking?: boolean;
/** @default false */
openTracking?: boolean;
publicKey: string;
dkimStatus?: string | null;
spfDetails?: string | null;
createdAt: string;
updatedAt: string;
/** @default false */
dmarcAdded?: boolean;
/** @default false */
isVerifying?: boolean;
errorMessage?: string | null;
subdomain?: string | null;
};
};
};
};
};
};
"/v1/domains/{id}/verify": {
put: {
parameters: {
path: {
id: number;
};
};
responses: {
/** @description Create a new domain */
200: {
content: {
"application/json": {
message: string;
};
};
};
};
};
}; };
"/v1/emails/{emailId}": { "/v1/emails/{emailId}": {
get: { get: {