Files
WeHub Mirror 6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
WeHub snapshot of cb28d14c6f2c081de7a0d8729a8c816c9adef67a
2026-08-10 11:17:50 +08:00

123 lines
3.7 KiB
TypeScript

import { z } from 'zod'
import { defineRouteContract } from '@/lib/api/contracts/types'
import { FileInputSchema } from '@/lib/uploads/utils/file-schemas'
/**
* Internal contracts for the Buffer create-post / edit-post routes. The Buffer
* GraphQL API attaches media by publicly accessible URL only, so the tools post
* a JSON envelope to these internal routes, which verify access to any
* referenced file, mint a short-lived presigned URL for it, and forward the
* mutation to Buffer.
*/
const postErrorSchema = z.object({
message: z.string(),
supportUrl: z.string().nullable(),
rawError: z.string().nullable(),
})
const postAssetSchema = z.object({
id: z.string().nullable(),
type: z.string(),
mimeType: z.string(),
source: z.string(),
thumbnail: z.string(),
})
const postSchema = z.object({
id: z.string(),
text: z.string(),
status: z.string(),
via: z.string(),
channelId: z.string(),
channelService: z.string(),
schedulingType: z.string().nullable(),
shareMode: z.string(),
isCustomScheduled: z.boolean(),
sharedNow: z.boolean(),
createdAt: z.string(),
updatedAt: z.string(),
dueAt: z.string().nullable(),
sentAt: z.string().nullable(),
externalLink: z.string().nullable(),
error: postErrorSchema.nullable(),
assets: z.array(postAssetSchema),
})
const postRouteResponseSchema = z.object({
success: z.boolean(),
output: z.object({ post: postSchema }).optional(),
error: z.string().optional(),
})
const postSharedFields = {
apiKey: z.string().min(1, 'API key is required'),
text: z.string().max(50000, 'text is too long').optional().nullable(),
mode: z.enum(['addToQueue', 'shareNext', 'shareNow', 'customScheduled']),
schedulingType: z.enum(['automatic', 'notification']).default('automatic'),
dueAt: z
.string()
.datetime({ offset: true, message: 'dueAt must be an ISO 8601 timestamp' })
.optional()
.nullable(),
saveToDraft: z.boolean().optional().nullable(),
media: FileInputSchema.optional().nullable(),
mediaType: z.enum(['auto', 'image', 'video']).default('auto'),
mediaAltText: z.string().max(1000, 'mediaAltText is too long').optional().nullable(),
}
/**
* Cross-field rule shared by create and edit: a custom-scheduled post needs a
* publish time.
*/
function validateDueAt(body: { mode: string; dueAt?: string | null }, ctx: z.RefinementCtx): void {
if (body.mode === 'customScheduled' && !body.dueAt) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['dueAt'],
message: 'dueAt is required when mode is customScheduled',
})
}
}
export const bufferCreatePostBodySchema = z
.object({
...postSharedFields,
channelId: z.string().min(1, 'channelId is required'),
})
.superRefine((body, ctx) => {
validateDueAt(body, ctx)
if (!body.text?.trim() && !body.media) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['text'],
message: 'Either text or media is required',
})
}
})
export type BufferCreatePostBody = z.input<typeof bufferCreatePostBodySchema>
export const bufferCreatePostContract = defineRouteContract({
method: 'POST',
path: '/api/tools/buffer/create-post',
body: bufferCreatePostBodySchema,
response: { mode: 'json', schema: postRouteResponseSchema },
})
export const bufferEditPostBodySchema = z
.object({
...postSharedFields,
postId: z.string().min(1, 'postId is required'),
})
.superRefine(validateDueAt)
export type BufferEditPostBody = z.input<typeof bufferEditPostBodySchema>
export const bufferEditPostContract = defineRouteContract({
method: 'POST',
path: '/api/tools/buffer/edit-post',
body: bufferEditPostBodySchema,
response: { mode: 'json', schema: postRouteResponseSchema },
})