web & sdk: improved sdk (better failover and batching)

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-01-12 11:31:46 +01:00
parent 8e7558790e
commit 5b5ad23f66
26 changed files with 1266 additions and 377 deletions

View File

@@ -4,16 +4,20 @@ import { createError, handleError } from '@/server/exceptions';
import type { NextApiRequest, NextApiResponse } from 'next';
import randomAnimalName from 'random-animal-name';
import type {
CreateProfilePayload,
CreateProfileResponse,
ProfilePayload,
} from '@mixan/types';
interface Request extends NextApiRequest {
body: {
id?: string;
properties?: Record<string, any>;
};
body: ProfilePayload | CreateProfilePayload;
}
export default async function handler(req: Request, res: NextApiResponse) {
if (req.method == 'OPTIONS') {
return res.status(202).json({});
await validateSdkRequest(req, res);
return res.status(200).json({});
}
if (req.method !== 'POST') {
@@ -22,12 +26,15 @@ export default async function handler(req: Request, res: NextApiResponse) {
try {
// Check client id & secret
const projectId = await validateSdkRequest(req);
const projectId = await validateSdkRequest(req, res);
// Providing an `ID` is deprecated, should be removed in the future
const profileId = 'id' in req.body ? req.body.id : undefined;
const { properties } = req.body ?? {};
const { id, properties } = req.body ?? {};
const profile = await db.profile.create({
data: {
id,
id: profileId,
external_id: null,
email: null,
first_name: randomAnimalName(),
@@ -40,7 +47,8 @@ export default async function handler(req: Request, res: NextApiResponse) {
},
});
res.status(200).json({ id: profile.id });
const response: CreateProfileResponse = { id: profile.id };
res.status(200).json(response);
} catch (error) {
handleError(res, error);
}