@jskit-ai/rewarded-core 0.1.120

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,167 @@
1
+ import { normalizeSurfaceId } from "@jskit-ai/kernel/shared/surface/registry";
2
+ import { checkRouteVisibility } from "@jskit-ai/kernel/shared/support/visibility";
3
+ import { resolveScopedApiBasePath } from "@jskit-ai/kernel/shared/surface";
4
+ import {
5
+ createTransportResponseSchema,
6
+ withStandardErrorResponses
7
+ } from "@jskit-ai/http-runtime/shared/validators/errorResponses";
8
+ import { routeParamsValidator } from "@jskit-ai/workspaces-core/server/validators/routeParamsValidator";
9
+ import { buildWorkspaceInputFromRouteParams } from "@jskit-ai/workspaces-core/server/support/workspaceRouteInput";
10
+ import {
11
+ ACTION_CURRENT,
12
+ ACTION_START,
13
+ ACTION_GRANT,
14
+ ACTION_CLOSE
15
+ } from "./actions.js";
16
+ import {
17
+ currentQueryInputValidator,
18
+ startCommandInputValidator,
19
+ grantCommandInputValidator,
20
+ closeCommandInputValidator,
21
+ currentStateOutputValidator,
22
+ startGateOutputValidator,
23
+ grantRewardOutputValidator,
24
+ closeSessionOutputValidator
25
+ } from "./inputSchemas.js";
26
+
27
+ function createWorkflowResponses(outputValidator) {
28
+ return withStandardErrorResponses({
29
+ 200: createTransportResponseSchema(
30
+ outputValidator.schema.toJsonSchema({
31
+ mode: outputValidator.mode
32
+ })
33
+ )
34
+ }, {
35
+ includeValidation400: true
36
+ });
37
+ }
38
+
39
+ function registerRoutes(
40
+ router,
41
+ {
42
+ routeOwnershipFilter = "workspace_user",
43
+ routeSurface = "app",
44
+ routeRelativePath = "rewarded"
45
+ } = {}
46
+ ) {
47
+ if (!router || typeof router.register !== "function") {
48
+ throw new Error("registerRoutes requires an HTTP router.");
49
+ }
50
+ const normalizedRouteSurface = normalizeSurfaceId(routeSurface);
51
+ const routeBase = resolveScopedApiBasePath({
52
+ routeBase: "/w/:workspaceSlug",
53
+ relativePath: routeRelativePath,
54
+ strictParams: false
55
+ });
56
+ const visibility = checkRouteVisibility(routeOwnershipFilter);
57
+
58
+ router.register(
59
+ "GET",
60
+ `${routeBase}/current`,
61
+ {
62
+ auth: "required",
63
+ surface: normalizedRouteSurface,
64
+ visibility,
65
+ params: routeParamsValidator,
66
+ query: currentQueryInputValidator,
67
+ responses: createWorkflowResponses(currentStateOutputValidator),
68
+ meta: {
69
+ tags: ["rewarded"],
70
+ summary: "Read the current Rewarded gate state."
71
+ }
72
+ },
73
+ async function rewardedCurrentRoute(request, reply) {
74
+ const response = await request.executeAction({
75
+ actionId: ACTION_CURRENT,
76
+ input: {
77
+ ...buildWorkspaceInputFromRouteParams(request.input.params),
78
+ ...(request.input.query || {})
79
+ }
80
+ });
81
+ reply.code(200).send(response);
82
+ }
83
+ );
84
+
85
+ router.register(
86
+ "POST",
87
+ `${routeBase}/start`,
88
+ {
89
+ auth: "required",
90
+ surface: normalizedRouteSurface,
91
+ visibility,
92
+ params: routeParamsValidator,
93
+ body: startCommandInputValidator,
94
+ responses: createWorkflowResponses(startGateOutputValidator),
95
+ meta: {
96
+ tags: ["rewarded"],
97
+ summary: "Start a Rewarded watch session."
98
+ }
99
+ },
100
+ async function rewardedStartRoute(request, reply) {
101
+ const response = await request.executeAction({
102
+ actionId: ACTION_START,
103
+ input: {
104
+ ...buildWorkspaceInputFromRouteParams(request.input.params),
105
+ ...(request.input.body || {})
106
+ }
107
+ });
108
+ reply.code(200).send(response);
109
+ }
110
+ );
111
+
112
+ router.register(
113
+ "POST",
114
+ `${routeBase}/grant`,
115
+ {
116
+ auth: "required",
117
+ surface: normalizedRouteSurface,
118
+ visibility,
119
+ params: routeParamsValidator,
120
+ body: grantCommandInputValidator,
121
+ responses: createWorkflowResponses(grantRewardOutputValidator),
122
+ meta: {
123
+ tags: ["rewarded"],
124
+ summary: "Grant a Rewarded unlock after a rewarded ad completes."
125
+ }
126
+ },
127
+ async function rewardedGrantRoute(request, reply) {
128
+ const response = await request.executeAction({
129
+ actionId: ACTION_GRANT,
130
+ input: {
131
+ ...buildWorkspaceInputFromRouteParams(request.input.params),
132
+ ...(request.input.body || {})
133
+ }
134
+ });
135
+ reply.code(200).send(response);
136
+ }
137
+ );
138
+
139
+ router.register(
140
+ "POST",
141
+ `${routeBase}/close`,
142
+ {
143
+ auth: "required",
144
+ surface: normalizedRouteSurface,
145
+ visibility,
146
+ params: routeParamsValidator,
147
+ body: closeCommandInputValidator,
148
+ responses: createWorkflowResponses(closeSessionOutputValidator),
149
+ meta: {
150
+ tags: ["rewarded"],
151
+ summary: "Close a Rewarded watch session without granting a reward."
152
+ }
153
+ },
154
+ async function rewardedCloseRoute(request, reply) {
155
+ const response = await request.executeAction({
156
+ actionId: ACTION_CLOSE,
157
+ input: {
158
+ ...buildWorkspaceInputFromRouteParams(request.input.params),
159
+ ...(request.input.body || {})
160
+ }
161
+ });
162
+ reply.code(200).send(response);
163
+ }
164
+ );
165
+ }
166
+
167
+ export { registerRoutes };