Skip to content

Commit d3a8a71

Browse files
committed
feat: add common types pkg
1 parent 1c6ebfe commit d3a8a71

9 files changed

Lines changed: 409 additions & 0 deletions

File tree

packages/types/package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "@refref/types",
3+
"version": "0.0.1",
4+
"private": true,
5+
"main": "./dist/index.js",
6+
"module": "./dist/index.mjs",
7+
"types": "./dist/index.d.ts",
8+
"files": [
9+
"dist/**"
10+
],
11+
"scripts": {
12+
"build": "tsup",
13+
"dev": "tsup --watch",
14+
"lint": "eslint .",
15+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
16+
},
17+
"devDependencies": {
18+
"@refref/typescript-config": "workspace:*",
19+
"eslint": "^8.56.0",
20+
"tsup": "^8.0.2",
21+
"typescript": "^5.3.3"
22+
},
23+
"dependencies": {
24+
"zod": "^3.24.2"
25+
}
26+
}

packages/types/src/event-config.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { z } from "zod";
2+
3+
// Event Definition Config V1
4+
export const eventDefinitionConfigV1Schema = z.object({
5+
schemaVersion: z.literal(1),
6+
requiredFields: z.array(z.string()).optional(),
7+
validationRules: z.record(z.unknown()).optional(),
8+
});
9+
export type EventDefinitionConfigV1Type = z.infer<
10+
typeof eventDefinitionConfigV1Schema
11+
>;
12+
13+
// Event Metadata V1
14+
export const eventMetadataV1Schema = z.object({
15+
schemaVersion: z.literal(1),
16+
source: z.enum(["auto", "api"]).optional(),
17+
reason: z.string().optional(),
18+
orderAmount: z.number().optional(),
19+
orderId: z.string().optional(),
20+
productIds: z.array(z.string()).optional(),
21+
customData: z.record(z.unknown()).optional(),
22+
});
23+
export type EventMetadataV1Type = z.infer<typeof eventMetadataV1Schema>;

packages/types/src/index.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { z } from "zod";
2+
import {
3+
socialPlatformSchema,
4+
widgetConfigSchema,
5+
WidgetConfigType,
6+
widgetPositionSchema,
7+
} from "./widget-config";
8+
9+
export * from "./program-config";
10+
export * from "./program-template-steps";
11+
export * from "./widget-config";
12+
export * from "./event-config";
13+
export * from "./reward-config";
14+
15+
export const referralLinkSchema = z.object({
16+
code: z.string(),
17+
url: z.string(),
18+
});
19+
export type ReferralLinkType = z.infer<typeof referralLinkSchema>;
20+
21+
export type WidgetInitResponseType = WidgetConfigType;
22+
23+
export interface WidgetStore {
24+
initialized: boolean;
25+
token: string | null | undefined;
26+
projectId: string | null | undefined;
27+
widgetElementSelector: string | null | undefined;
28+
isOpen: boolean;
29+
config: WidgetConfigType;
30+
participantId: string;
31+
referralLinks: Record<string, string>;
32+
setIsOpen: (isOpen: boolean) => void;
33+
setToken: (token: string | null | undefined) => void;
34+
setConfig: (config: Partial<WidgetConfigType>) => void;
35+
toggle: () => void;
36+
setReferralLinks: (links: Record<string, string>) => void;
37+
}
38+
39+
/**
40+
* Allowed keys for setup steps. Extend this union as needed for new step types.
41+
*/
42+
export type SetupStepKeyType = "company" | "brand" | "reward";
43+
44+
// ActionTypeConfig Zod schema and type
45+
export const actionTypeConfigSchema = z.object({
46+
schemaVersion: z.number(),
47+
tracking: z.object({
48+
method: z.enum(["automatic", "manual"]),
49+
}),
50+
verification: z.object({
51+
method: z.enum(["automatic", "manual"]),
52+
}),
53+
});
54+
export type ActionTypeConfigType = z.infer<typeof actionTypeConfigSchema>;
55+
56+
/**
57+
* JWT payload schema for authentication and authorization
58+
*/
59+
export const jwtPayloadSchema = z.object({
60+
sub: z.string(),
61+
email: z.string().email().optional(),
62+
name: z.string().optional(),
63+
projectId: z.string(),
64+
});
65+
export type JwtPayloadType = z.infer<typeof jwtPayloadSchema>;
66+
67+
export const widgetInitRequestSchema = z.object({
68+
projectId: z.string(),
69+
referralCode: z.string().optional(),
70+
});
71+
export type WidgetInitRequestType = z.infer<typeof widgetInitRequestSchema>;
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { z } from "zod";
2+
import { widgetConfigSchema } from "./widget-config";
3+
4+
export const programTemplateStepKeySchema = z.enum(["brand", "reward"]);
5+
export type ProgramTemplateStepKeyType = z.infer<
6+
typeof programTemplateStepKeySchema
7+
>;
8+
9+
// ProgramTemplateStepConfig Zod schema and type
10+
export const programTemplateStepConfigSchema = z.object({
11+
key: programTemplateStepKeySchema,
12+
title: z.string(),
13+
description: z.string().optional(),
14+
});
15+
export type ProgramTemplateStepConfigType = z.infer<
16+
typeof programTemplateStepConfigSchema
17+
>;
18+
19+
// ProgramTemplateConfig Zod schema and type
20+
export const programTemplateConfigSchema = z.object({
21+
schemaVersion: z.number(),
22+
steps: z.array(programTemplateStepConfigSchema),
23+
meta: z.record(z.unknown()).optional(),
24+
});
25+
export type ProgramTemplateConfigType = z.infer<
26+
typeof programTemplateConfigSchema
27+
>;
28+
29+
// --- Action Config Schemas ---
30+
31+
// Enums for sub-configs
32+
const trackingMethodEnum = z.enum([
33+
"unique_link",
34+
"coupon_code",
35+
"api",
36+
"manual",
37+
]);
38+
const verificationMethodEnum = z.enum(["automatic", "manual"]);
39+
40+
// Tracking configuration schema
41+
const trackingConfigSchema = z.object({
42+
method: trackingMethodEnum,
43+
urlSettings: z
44+
.object({
45+
allowCustom: z.boolean().optional(),
46+
domain: z.string().url().optional(),
47+
})
48+
.optional(),
49+
});
50+
51+
// Verification configuration schema
52+
const verificationConfigSchema = z.object({
53+
method: verificationMethodEnum,
54+
});
55+
56+
export const RewardCurrencies = ["USD", "EUR", "GBP"] as const;
57+
58+
export const currencySchema = z.enum(RewardCurrencies);
59+
60+
export type CurrencyType = z.infer<typeof currencySchema>;
61+
62+
// Simplified reward configuration for initial implementation
63+
// Only supporting cash rewards for referrers and discounts for referees
64+
export const rewardConfigSchema = z.object({
65+
referrer: z.object({
66+
type: z.literal("cash"),
67+
valueType: z.enum(["fixed", "percentage"]),
68+
value: z.number().positive(),
69+
currency: currencySchema,
70+
}),
71+
referee: z.object({
72+
type: z.literal("discount"),
73+
valueType: z.enum(["fixed", "percentage"]),
74+
value: z.number().positive(),
75+
currency: currencySchema.optional(), // Only for fixed discounts
76+
minPurchaseAmount: z.number().positive().optional(),
77+
validityDays: z.number().int().positive().optional(),
78+
}),
79+
});
80+
81+
export type RewardConfigType = z.infer<typeof rewardConfigSchema>;
82+
83+
// Notification schema
84+
export const notificationConfigV1Schema = z
85+
.object({
86+
// Add notification config fields when implemented
87+
})
88+
.optional();
89+
export type NotificationConfigV1Type = z.infer<
90+
typeof notificationConfigV1Schema
91+
>;
92+
93+
// Program config schema
94+
export const programConfigV1Schema = z.object({
95+
schemaVersion: z.literal(1),
96+
actions: z.array(z.any()).optional(), // Legacy field, being phased out
97+
notification: notificationConfigV1Schema,
98+
templateConfig: programTemplateConfigSchema.optional(),
99+
widgetConfig: widgetConfigSchema,
100+
});
101+
102+
export type ProgramConfigV1Type = z.infer<typeof programConfigV1Schema>;
103+
104+
export const configuredProgramTemplateSchema = z.object({
105+
rewardConfig: rewardConfigSchema.optional(), // New reward configuration
106+
});
107+
export type ConfiguredProgramTemplateType = z.infer<
108+
typeof configuredProgramTemplateSchema
109+
>;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { z } from "zod";
2+
import { currencySchema } from "./program-config";
3+
4+
// New simplified reward step schema
5+
export const programTemplateRewardStepSchemaV2 = z.object({
6+
referrerReward: z.object({
7+
enabled: z.boolean(),
8+
valueType: z.enum(["fixed", "percentage"]),
9+
value: z.number().positive(),
10+
currency: currencySchema,
11+
}),
12+
refereeReward: z.object({
13+
enabled: z.boolean(),
14+
valueType: z.enum(["fixed", "percentage"]),
15+
value: z.number().positive(),
16+
currency: currencySchema.optional(), // Only for fixed discounts
17+
minPurchaseAmount: z.number().positive().optional(),
18+
validityDays: z.number().int().positive().optional(),
19+
}),
20+
});
21+
22+
export type ProgramTemplateRewardStepV2Type = z.infer<
23+
typeof programTemplateRewardStepSchemaV2
24+
>;
25+
26+
// Keep old schema for backward compatibility
27+
/** @deprecated Use programTemplateRewardStepSchemaV2 instead */
28+
export const programTemplateRewardStepSchema = z.object({
29+
type: z.literal("cash"),
30+
defaultCurrency: z.string().min(1),
31+
rewardType: z.enum(["percentage", "fixed"]),
32+
revenueSharePercentage: z.number().min(0).max(50),
33+
revenueSharePeriodType: z.literal("lifetime"),
34+
cap: z.number().min(0),
35+
newUserRewardType: z.enum(["percentage", "fixed"]),
36+
newUserRewardMonths: z.number().min(1),
37+
newUserRewardValue: z.number().min(0),
38+
});
39+
40+
/** @deprecated Use ProgramTemplateRewardStepV2Type instead */
41+
export type ProgramTemplateRewardStepType = z.infer<
42+
typeof programTemplateRewardStepSchema
43+
>;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { z } from "zod";
2+
3+
// Reward types enum (only cash and discount)
4+
export const rewardTypeSchema = z.enum(["cash", "discount"]);
5+
export type RewardTypeEnum = z.infer<typeof rewardTypeSchema>;
6+
7+
// Participant type enum
8+
export const participantTypeSchema = z.enum(["referrer", "referee"]);
9+
export type ParticipantTypeEnum = z.infer<typeof participantTypeSchema>;
10+
11+
// Reward Rule Config V1 (simplified - event triggers only)
12+
export const rewardRuleConfigV1Schema = z.object({
13+
schemaVersion: z.literal(1),
14+
trigger: z.object({
15+
event: z.string(), // "purchase", "signup", etc.
16+
}),
17+
participantType: participantTypeSchema,
18+
reward: z.object({
19+
type: rewardTypeSchema,
20+
amount: z.number(),
21+
unit: z.enum(["fixed", "percent"]).optional(), // for discount: percent, for cash: fixed
22+
}),
23+
});
24+
export type RewardRuleConfigV1Type = z.infer<typeof rewardRuleConfigV1Schema>;
25+
26+
// Reward Metadata V1 (simplified for cash and discount only)
27+
export const rewardMetadataV1Schema = z.object({
28+
schemaVersion: z.literal(1),
29+
// For discount rewards
30+
couponCode: z.string().optional(),
31+
validUntil: z.string().optional(), // ISO date string
32+
minPurchaseAmount: z.number().optional(),
33+
// General
34+
notes: z.string().optional(),
35+
customData: z.record(z.unknown()).optional(),
36+
});
37+
export type RewardMetadataV1Type = z.infer<typeof rewardMetadataV1Schema>;

0 commit comments

Comments
 (0)