@cmssy/next 0.2.1 → 0.2.3
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.
- package/dist/index.cjs +161 -5
- package/dist/index.d.cts +26 -2
- package/dist/index.d.ts +26 -2
- package/dist/index.js +159 -6
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -221,8 +221,8 @@ async function splitCmssyLocale(config, path) {
|
|
|
221
221
|
return react.splitLocaleFromPath(path, siteLocales);
|
|
222
222
|
}
|
|
223
223
|
async function getCmssyLocale(config) {
|
|
224
|
-
const { headers:
|
|
225
|
-
const headerList = await
|
|
224
|
+
const { headers: headers3 } = await import('next/headers');
|
|
225
|
+
const headerList = await headers3();
|
|
226
226
|
const fromHeader = headerList.get(CMSSY_LOCALE_HEADER);
|
|
227
227
|
if (fromHeader) return fromHeader;
|
|
228
228
|
const { defaultLocale } = await react.resolveSiteLocales(config);
|
|
@@ -362,11 +362,11 @@ function decodeAccessClaims(accessToken) {
|
|
|
362
362
|
try {
|
|
363
363
|
const base64 = parts[1].replace(/-/g, "+").replace(/_/g, "/");
|
|
364
364
|
const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
|
|
365
|
-
const
|
|
366
|
-
if (typeof
|
|
365
|
+
const json4 = JSON.parse(new TextDecoder().decode(bytes));
|
|
366
|
+
if (typeof json4.recordId !== "string" || typeof json4.email !== "string" || json4.type !== "site_member") {
|
|
367
367
|
return null;
|
|
368
368
|
}
|
|
369
|
-
return { recordId:
|
|
369
|
+
return { recordId: json4.recordId, email: json4.email };
|
|
370
370
|
} catch {
|
|
371
371
|
return null;
|
|
372
372
|
}
|
|
@@ -1078,6 +1078,159 @@ function createCmssyAuthMiddleware(config) {
|
|
|
1078
1078
|
return refreshed;
|
|
1079
1079
|
};
|
|
1080
1080
|
}
|
|
1081
|
+
var PRODUCTS_QUERY = `query Products($workspaceId: String!, $modelSlug: String!, $filter: JSON, $limit: Int) {
|
|
1082
|
+
publicModelRecords(workspaceId: $workspaceId, modelSlug: $modelSlug, filter: $filter, limit: $limit) {
|
|
1083
|
+
items { id data variants { id sku price inventory selectedOptions { name value } } }
|
|
1084
|
+
}
|
|
1085
|
+
}`;
|
|
1086
|
+
async function fetchProducts(config, options) {
|
|
1087
|
+
const workspaceId = await react.resolveWorkspaceId(config);
|
|
1088
|
+
const data = await react.graphqlRequest(
|
|
1089
|
+
config,
|
|
1090
|
+
PRODUCTS_QUERY,
|
|
1091
|
+
{
|
|
1092
|
+
workspaceId,
|
|
1093
|
+
modelSlug: options.modelSlug,
|
|
1094
|
+
filter: options.filter ?? {},
|
|
1095
|
+
limit: options.limit ?? 50
|
|
1096
|
+
},
|
|
1097
|
+
{ headers: { "x-workspace-id": workspaceId } },
|
|
1098
|
+
"products query"
|
|
1099
|
+
);
|
|
1100
|
+
return data.publicModelRecords.items;
|
|
1101
|
+
}
|
|
1102
|
+
async function fetchProduct(config, options) {
|
|
1103
|
+
const products = await fetchProducts(config, {
|
|
1104
|
+
modelSlug: options.modelSlug,
|
|
1105
|
+
filter: { [options.slugField ?? "slug"]: options.slug },
|
|
1106
|
+
limit: 1
|
|
1107
|
+
});
|
|
1108
|
+
return products[0] ?? null;
|
|
1109
|
+
}
|
|
1110
|
+
var ORDER_FIELDS = `
|
|
1111
|
+
id
|
|
1112
|
+
status
|
|
1113
|
+
subtotal
|
|
1114
|
+
tax
|
|
1115
|
+
total
|
|
1116
|
+
currency
|
|
1117
|
+
customerEmail
|
|
1118
|
+
refundedAmount
|
|
1119
|
+
paymentProvider
|
|
1120
|
+
paidAt
|
|
1121
|
+
fulfilledAt
|
|
1122
|
+
createdAt
|
|
1123
|
+
items { name price currency quantity sku }
|
|
1124
|
+
`;
|
|
1125
|
+
var MY_ORDERS = `query MyOrders($workspaceId: ID!, $skip: Int, $limit: Int) {
|
|
1126
|
+
myOrders(workspaceId: $workspaceId, skip: $skip, limit: $limit) {
|
|
1127
|
+
total
|
|
1128
|
+
hasMore
|
|
1129
|
+
items { ${ORDER_FIELDS} }
|
|
1130
|
+
}
|
|
1131
|
+
}`;
|
|
1132
|
+
var MY_ORDER = `query MyOrder($workspaceId: ID!, $id: ID!) {
|
|
1133
|
+
myOrder(workspaceId: $workspaceId, id: $id) { ${ORDER_FIELDS} }
|
|
1134
|
+
}`;
|
|
1135
|
+
var workspaceIdCache3 = /* @__PURE__ */ new Map();
|
|
1136
|
+
function workspaceIdFor3(config) {
|
|
1137
|
+
const key = `${config.apiUrl}::${config.workspaceSlug}`;
|
|
1138
|
+
const existing = workspaceIdCache3.get(key);
|
|
1139
|
+
if (existing) return existing;
|
|
1140
|
+
const fresh = react.resolveWorkspaceId(config).catch((err) => {
|
|
1141
|
+
workspaceIdCache3.delete(key);
|
|
1142
|
+
throw err;
|
|
1143
|
+
});
|
|
1144
|
+
workspaceIdCache3.set(key, fresh);
|
|
1145
|
+
return fresh;
|
|
1146
|
+
}
|
|
1147
|
+
function headers2(workspaceId, accessToken) {
|
|
1148
|
+
return {
|
|
1149
|
+
"x-workspace-id": workspaceId,
|
|
1150
|
+
authorization: `Bearer ${accessToken}`
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
async function backendMyOrders(config, accessToken, options) {
|
|
1154
|
+
const workspaceId = await workspaceIdFor3(config);
|
|
1155
|
+
const data = await react.graphqlRequest(
|
|
1156
|
+
config,
|
|
1157
|
+
MY_ORDERS,
|
|
1158
|
+
{ workspaceId, skip: options.skip, limit: options.limit },
|
|
1159
|
+
{ headers: headers2(workspaceId, accessToken) },
|
|
1160
|
+
"my orders"
|
|
1161
|
+
);
|
|
1162
|
+
return data.myOrders;
|
|
1163
|
+
}
|
|
1164
|
+
async function backendMyOrder(config, accessToken, id) {
|
|
1165
|
+
const workspaceId = await workspaceIdFor3(config);
|
|
1166
|
+
const data = await react.graphqlRequest(
|
|
1167
|
+
config,
|
|
1168
|
+
MY_ORDER,
|
|
1169
|
+
{ workspaceId, id },
|
|
1170
|
+
{ headers: headers2(workspaceId, accessToken) },
|
|
1171
|
+
"my order"
|
|
1172
|
+
);
|
|
1173
|
+
return data.myOrder;
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
// src/create-orders-route.ts
|
|
1177
|
+
var DEFAULT_LIMIT = 20;
|
|
1178
|
+
var MAX_LIMIT = 100;
|
|
1179
|
+
function json3(body, status = 200) {
|
|
1180
|
+
return new Response(JSON.stringify(body), {
|
|
1181
|
+
status,
|
|
1182
|
+
headers: {
|
|
1183
|
+
"content-type": "application/json",
|
|
1184
|
+
"cache-control": "no-store"
|
|
1185
|
+
}
|
|
1186
|
+
});
|
|
1187
|
+
}
|
|
1188
|
+
function createCmssyOrdersRoute(config) {
|
|
1189
|
+
async function memberAccessToken() {
|
|
1190
|
+
if (!config.auth) return void 0;
|
|
1191
|
+
const jar = await headers.cookies();
|
|
1192
|
+
const raw = jar.get(CMSSY_SESSION_COOKIE)?.value;
|
|
1193
|
+
if (!raw) return void 0;
|
|
1194
|
+
const session = await openSession(
|
|
1195
|
+
raw,
|
|
1196
|
+
config.auth.sessionSecret,
|
|
1197
|
+
config.workspaceSlug
|
|
1198
|
+
);
|
|
1199
|
+
if (!session || isAccessExpired(session)) return void 0;
|
|
1200
|
+
return session.accessToken;
|
|
1201
|
+
}
|
|
1202
|
+
return {
|
|
1203
|
+
async GET(request2) {
|
|
1204
|
+
const accessToken = await memberAccessToken();
|
|
1205
|
+
if (!accessToken) {
|
|
1206
|
+
return json3({ message: "Not signed in." }, 401);
|
|
1207
|
+
}
|
|
1208
|
+
const url = new URL(request2.url);
|
|
1209
|
+
try {
|
|
1210
|
+
const id = url.searchParams.get("id");
|
|
1211
|
+
if (id) {
|
|
1212
|
+
return json3({ order: await backendMyOrder(config, accessToken, id) });
|
|
1213
|
+
}
|
|
1214
|
+
const skip = Math.max(
|
|
1215
|
+
0,
|
|
1216
|
+
Math.floor(Number(url.searchParams.get("skip")) || 0)
|
|
1217
|
+
);
|
|
1218
|
+
const limitParam = Math.floor(Number(url.searchParams.get("limit")));
|
|
1219
|
+
const limit = Number.isFinite(limitParam) && limitParam > 0 ? Math.min(limitParam, MAX_LIMIT) : DEFAULT_LIMIT;
|
|
1220
|
+
const result = await backendMyOrders(config, accessToken, {
|
|
1221
|
+
skip,
|
|
1222
|
+
limit
|
|
1223
|
+
});
|
|
1224
|
+
return json3(result);
|
|
1225
|
+
} catch (err) {
|
|
1226
|
+
return json3(
|
|
1227
|
+
{ message: err instanceof Error ? err.message : "Orders error" },
|
|
1228
|
+
502
|
|
1229
|
+
);
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
};
|
|
1233
|
+
}
|
|
1081
1234
|
|
|
1082
1235
|
exports.CMSSY_CART_COOKIE = CMSSY_CART_COOKIE;
|
|
1083
1236
|
exports.CMSSY_EDIT_HEADER = CMSSY_EDIT_HEADER;
|
|
@@ -1090,8 +1243,11 @@ exports.cmssyCspHeaders = cmssyCspHeaders;
|
|
|
1090
1243
|
exports.createCmssyAuthMiddleware = createCmssyAuthMiddleware;
|
|
1091
1244
|
exports.createCmssyAuthRoute = createCmssyAuthRoute;
|
|
1092
1245
|
exports.createCmssyCartRoute = createCmssyCartRoute;
|
|
1246
|
+
exports.createCmssyOrdersRoute = createCmssyOrdersRoute;
|
|
1093
1247
|
exports.createCmssyPage = createCmssyPage;
|
|
1094
1248
|
exports.createDraftRoute = createDraftRoute;
|
|
1249
|
+
exports.fetchProduct = fetchProduct;
|
|
1250
|
+
exports.fetchProducts = fetchProducts;
|
|
1095
1251
|
exports.getCmssyAccessToken = getCmssyAccessToken;
|
|
1096
1252
|
exports.getCmssyLocale = getCmssyLocale;
|
|
1097
1253
|
exports.getCmssyUser = getCmssyUser;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import { ComponentType } from 'react';
|
|
3
|
-
import { CmssyPageData, CmssyFormDefinition, BlockDefinition, CmssyClientConfig } from '@cmssy/react';
|
|
3
|
+
import { CmssyPageData, CmssyFormDefinition, BlockDefinition, CmssyClientConfig, CmssyProduct, CmssyOrder } from '@cmssy/react';
|
|
4
4
|
import { EditBridgeConfig } from '@cmssy/react/client';
|
|
5
5
|
import { NextRequest, NextResponse } from 'next/server';
|
|
6
6
|
|
|
@@ -136,4 +136,28 @@ declare function getCmssyAccessToken(config: CmssyNextConfig): Promise<string |
|
|
|
136
136
|
type CmssyAuthMiddleware = (request: NextRequest) => Promise<NextResponse>;
|
|
137
137
|
declare function createCmssyAuthMiddleware(config: CmssyNextConfig): CmssyAuthMiddleware;
|
|
138
138
|
|
|
139
|
-
|
|
139
|
+
interface FetchProductsOptions {
|
|
140
|
+
modelSlug: string;
|
|
141
|
+
filter?: Record<string, unknown>;
|
|
142
|
+
limit?: number;
|
|
143
|
+
}
|
|
144
|
+
declare function fetchProducts(config: CmssyNextConfig, options: FetchProductsOptions): Promise<CmssyProduct[]>;
|
|
145
|
+
interface FetchProductOptions {
|
|
146
|
+
modelSlug: string;
|
|
147
|
+
slug: string;
|
|
148
|
+
slugField?: string;
|
|
149
|
+
}
|
|
150
|
+
declare function fetchProduct(config: CmssyNextConfig, options: FetchProductOptions): Promise<CmssyProduct | null>;
|
|
151
|
+
|
|
152
|
+
interface CmssyOrdersRouteHandlers {
|
|
153
|
+
GET(request: Request): Promise<Response>;
|
|
154
|
+
}
|
|
155
|
+
declare function createCmssyOrdersRoute(config: CmssyNextConfig): CmssyOrdersRouteHandlers;
|
|
156
|
+
|
|
157
|
+
interface MyOrdersResult {
|
|
158
|
+
items: CmssyOrder[];
|
|
159
|
+
total: number;
|
|
160
|
+
hasMore: boolean;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export { CMSSY_CART_COOKIE, CMSSY_EDIT_HEADER, CMSSY_LOCALE_HEADER, CMSSY_SESSION_COOKIE, type CmssyAuthConfig, type CmssyAuthMiddleware, type CmssyAuthRouteHandlers, type CmssyCartRouteHandlers, type CmssyCspOptions, type CmssyDraftRouteConfig, type CmssyEditorProps, type CmssyNextConfig, type CmssyOrdersRouteHandlers, type CmssySessionPayload, type CmssySessionUser, type CreateCmssyPageOptions, type FetchProductOptions, type FetchProductsOptions, type MyOrdersResult, SESSION_MAX_AGE_SECONDS, type SessionCookieOptions, applyCmssyCsp, assertAuthConfig, cmssyCspHeaders, createCmssyAuthMiddleware, createCmssyAuthRoute, createCmssyCartRoute, createCmssyOrdersRoute, createCmssyPage, createDraftRoute, fetchProduct, fetchProducts, getCmssyAccessToken, getCmssyLocale, getCmssyUser, isAccessExpired, isCmssyEditMode, isCmssyEditRequest, localeForPathname, openSession, sealSession, sessionCookieOptions, splitCmssyLocale };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import { ComponentType } from 'react';
|
|
3
|
-
import { CmssyPageData, CmssyFormDefinition, BlockDefinition, CmssyClientConfig } from '@cmssy/react';
|
|
3
|
+
import { CmssyPageData, CmssyFormDefinition, BlockDefinition, CmssyClientConfig, CmssyProduct, CmssyOrder } from '@cmssy/react';
|
|
4
4
|
import { EditBridgeConfig } from '@cmssy/react/client';
|
|
5
5
|
import { NextRequest, NextResponse } from 'next/server';
|
|
6
6
|
|
|
@@ -136,4 +136,28 @@ declare function getCmssyAccessToken(config: CmssyNextConfig): Promise<string |
|
|
|
136
136
|
type CmssyAuthMiddleware = (request: NextRequest) => Promise<NextResponse>;
|
|
137
137
|
declare function createCmssyAuthMiddleware(config: CmssyNextConfig): CmssyAuthMiddleware;
|
|
138
138
|
|
|
139
|
-
|
|
139
|
+
interface FetchProductsOptions {
|
|
140
|
+
modelSlug: string;
|
|
141
|
+
filter?: Record<string, unknown>;
|
|
142
|
+
limit?: number;
|
|
143
|
+
}
|
|
144
|
+
declare function fetchProducts(config: CmssyNextConfig, options: FetchProductsOptions): Promise<CmssyProduct[]>;
|
|
145
|
+
interface FetchProductOptions {
|
|
146
|
+
modelSlug: string;
|
|
147
|
+
slug: string;
|
|
148
|
+
slugField?: string;
|
|
149
|
+
}
|
|
150
|
+
declare function fetchProduct(config: CmssyNextConfig, options: FetchProductOptions): Promise<CmssyProduct | null>;
|
|
151
|
+
|
|
152
|
+
interface CmssyOrdersRouteHandlers {
|
|
153
|
+
GET(request: Request): Promise<Response>;
|
|
154
|
+
}
|
|
155
|
+
declare function createCmssyOrdersRoute(config: CmssyNextConfig): CmssyOrdersRouteHandlers;
|
|
156
|
+
|
|
157
|
+
interface MyOrdersResult {
|
|
158
|
+
items: CmssyOrder[];
|
|
159
|
+
total: number;
|
|
160
|
+
hasMore: boolean;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export { CMSSY_CART_COOKIE, CMSSY_EDIT_HEADER, CMSSY_LOCALE_HEADER, CMSSY_SESSION_COOKIE, type CmssyAuthConfig, type CmssyAuthMiddleware, type CmssyAuthRouteHandlers, type CmssyCartRouteHandlers, type CmssyCspOptions, type CmssyDraftRouteConfig, type CmssyEditorProps, type CmssyNextConfig, type CmssyOrdersRouteHandlers, type CmssySessionPayload, type CmssySessionUser, type CreateCmssyPageOptions, type FetchProductOptions, type FetchProductsOptions, type MyOrdersResult, SESSION_MAX_AGE_SECONDS, type SessionCookieOptions, applyCmssyCsp, assertAuthConfig, cmssyCspHeaders, createCmssyAuthMiddleware, createCmssyAuthRoute, createCmssyCartRoute, createCmssyOrdersRoute, createCmssyPage, createDraftRoute, fetchProduct, fetchProducts, getCmssyAccessToken, getCmssyLocale, getCmssyUser, isAccessExpired, isCmssyEditMode, isCmssyEditRequest, localeForPathname, openSession, sealSession, sessionCookieOptions, splitCmssyLocale };
|
package/dist/index.js
CHANGED
|
@@ -219,8 +219,8 @@ async function splitCmssyLocale(config, path) {
|
|
|
219
219
|
return splitLocaleFromPath(path, siteLocales);
|
|
220
220
|
}
|
|
221
221
|
async function getCmssyLocale(config) {
|
|
222
|
-
const { headers:
|
|
223
|
-
const headerList = await
|
|
222
|
+
const { headers: headers3 } = await import('next/headers');
|
|
223
|
+
const headerList = await headers3();
|
|
224
224
|
const fromHeader = headerList.get(CMSSY_LOCALE_HEADER);
|
|
225
225
|
if (fromHeader) return fromHeader;
|
|
226
226
|
const { defaultLocale } = await resolveSiteLocales(config);
|
|
@@ -360,11 +360,11 @@ function decodeAccessClaims(accessToken) {
|
|
|
360
360
|
try {
|
|
361
361
|
const base64 = parts[1].replace(/-/g, "+").replace(/_/g, "/");
|
|
362
362
|
const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
|
|
363
|
-
const
|
|
364
|
-
if (typeof
|
|
363
|
+
const json4 = JSON.parse(new TextDecoder().decode(bytes));
|
|
364
|
+
if (typeof json4.recordId !== "string" || typeof json4.email !== "string" || json4.type !== "site_member") {
|
|
365
365
|
return null;
|
|
366
366
|
}
|
|
367
|
-
return { recordId:
|
|
367
|
+
return { recordId: json4.recordId, email: json4.email };
|
|
368
368
|
} catch {
|
|
369
369
|
return null;
|
|
370
370
|
}
|
|
@@ -1076,5 +1076,158 @@ function createCmssyAuthMiddleware(config) {
|
|
|
1076
1076
|
return refreshed;
|
|
1077
1077
|
};
|
|
1078
1078
|
}
|
|
1079
|
+
var PRODUCTS_QUERY = `query Products($workspaceId: String!, $modelSlug: String!, $filter: JSON, $limit: Int) {
|
|
1080
|
+
publicModelRecords(workspaceId: $workspaceId, modelSlug: $modelSlug, filter: $filter, limit: $limit) {
|
|
1081
|
+
items { id data variants { id sku price inventory selectedOptions { name value } } }
|
|
1082
|
+
}
|
|
1083
|
+
}`;
|
|
1084
|
+
async function fetchProducts(config, options) {
|
|
1085
|
+
const workspaceId = await resolveWorkspaceId(config);
|
|
1086
|
+
const data = await graphqlRequest(
|
|
1087
|
+
config,
|
|
1088
|
+
PRODUCTS_QUERY,
|
|
1089
|
+
{
|
|
1090
|
+
workspaceId,
|
|
1091
|
+
modelSlug: options.modelSlug,
|
|
1092
|
+
filter: options.filter ?? {},
|
|
1093
|
+
limit: options.limit ?? 50
|
|
1094
|
+
},
|
|
1095
|
+
{ headers: { "x-workspace-id": workspaceId } },
|
|
1096
|
+
"products query"
|
|
1097
|
+
);
|
|
1098
|
+
return data.publicModelRecords.items;
|
|
1099
|
+
}
|
|
1100
|
+
async function fetchProduct(config, options) {
|
|
1101
|
+
const products = await fetchProducts(config, {
|
|
1102
|
+
modelSlug: options.modelSlug,
|
|
1103
|
+
filter: { [options.slugField ?? "slug"]: options.slug },
|
|
1104
|
+
limit: 1
|
|
1105
|
+
});
|
|
1106
|
+
return products[0] ?? null;
|
|
1107
|
+
}
|
|
1108
|
+
var ORDER_FIELDS = `
|
|
1109
|
+
id
|
|
1110
|
+
status
|
|
1111
|
+
subtotal
|
|
1112
|
+
tax
|
|
1113
|
+
total
|
|
1114
|
+
currency
|
|
1115
|
+
customerEmail
|
|
1116
|
+
refundedAmount
|
|
1117
|
+
paymentProvider
|
|
1118
|
+
paidAt
|
|
1119
|
+
fulfilledAt
|
|
1120
|
+
createdAt
|
|
1121
|
+
items { name price currency quantity sku }
|
|
1122
|
+
`;
|
|
1123
|
+
var MY_ORDERS = `query MyOrders($workspaceId: ID!, $skip: Int, $limit: Int) {
|
|
1124
|
+
myOrders(workspaceId: $workspaceId, skip: $skip, limit: $limit) {
|
|
1125
|
+
total
|
|
1126
|
+
hasMore
|
|
1127
|
+
items { ${ORDER_FIELDS} }
|
|
1128
|
+
}
|
|
1129
|
+
}`;
|
|
1130
|
+
var MY_ORDER = `query MyOrder($workspaceId: ID!, $id: ID!) {
|
|
1131
|
+
myOrder(workspaceId: $workspaceId, id: $id) { ${ORDER_FIELDS} }
|
|
1132
|
+
}`;
|
|
1133
|
+
var workspaceIdCache3 = /* @__PURE__ */ new Map();
|
|
1134
|
+
function workspaceIdFor3(config) {
|
|
1135
|
+
const key = `${config.apiUrl}::${config.workspaceSlug}`;
|
|
1136
|
+
const existing = workspaceIdCache3.get(key);
|
|
1137
|
+
if (existing) return existing;
|
|
1138
|
+
const fresh = resolveWorkspaceId(config).catch((err) => {
|
|
1139
|
+
workspaceIdCache3.delete(key);
|
|
1140
|
+
throw err;
|
|
1141
|
+
});
|
|
1142
|
+
workspaceIdCache3.set(key, fresh);
|
|
1143
|
+
return fresh;
|
|
1144
|
+
}
|
|
1145
|
+
function headers2(workspaceId, accessToken) {
|
|
1146
|
+
return {
|
|
1147
|
+
"x-workspace-id": workspaceId,
|
|
1148
|
+
authorization: `Bearer ${accessToken}`
|
|
1149
|
+
};
|
|
1150
|
+
}
|
|
1151
|
+
async function backendMyOrders(config, accessToken, options) {
|
|
1152
|
+
const workspaceId = await workspaceIdFor3(config);
|
|
1153
|
+
const data = await graphqlRequest(
|
|
1154
|
+
config,
|
|
1155
|
+
MY_ORDERS,
|
|
1156
|
+
{ workspaceId, skip: options.skip, limit: options.limit },
|
|
1157
|
+
{ headers: headers2(workspaceId, accessToken) },
|
|
1158
|
+
"my orders"
|
|
1159
|
+
);
|
|
1160
|
+
return data.myOrders;
|
|
1161
|
+
}
|
|
1162
|
+
async function backendMyOrder(config, accessToken, id) {
|
|
1163
|
+
const workspaceId = await workspaceIdFor3(config);
|
|
1164
|
+
const data = await graphqlRequest(
|
|
1165
|
+
config,
|
|
1166
|
+
MY_ORDER,
|
|
1167
|
+
{ workspaceId, id },
|
|
1168
|
+
{ headers: headers2(workspaceId, accessToken) },
|
|
1169
|
+
"my order"
|
|
1170
|
+
);
|
|
1171
|
+
return data.myOrder;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
// src/create-orders-route.ts
|
|
1175
|
+
var DEFAULT_LIMIT = 20;
|
|
1176
|
+
var MAX_LIMIT = 100;
|
|
1177
|
+
function json3(body, status = 200) {
|
|
1178
|
+
return new Response(JSON.stringify(body), {
|
|
1179
|
+
status,
|
|
1180
|
+
headers: {
|
|
1181
|
+
"content-type": "application/json",
|
|
1182
|
+
"cache-control": "no-store"
|
|
1183
|
+
}
|
|
1184
|
+
});
|
|
1185
|
+
}
|
|
1186
|
+
function createCmssyOrdersRoute(config) {
|
|
1187
|
+
async function memberAccessToken() {
|
|
1188
|
+
if (!config.auth) return void 0;
|
|
1189
|
+
const jar = await cookies();
|
|
1190
|
+
const raw = jar.get(CMSSY_SESSION_COOKIE)?.value;
|
|
1191
|
+
if (!raw) return void 0;
|
|
1192
|
+
const session = await openSession(
|
|
1193
|
+
raw,
|
|
1194
|
+
config.auth.sessionSecret,
|
|
1195
|
+
config.workspaceSlug
|
|
1196
|
+
);
|
|
1197
|
+
if (!session || isAccessExpired(session)) return void 0;
|
|
1198
|
+
return session.accessToken;
|
|
1199
|
+
}
|
|
1200
|
+
return {
|
|
1201
|
+
async GET(request2) {
|
|
1202
|
+
const accessToken = await memberAccessToken();
|
|
1203
|
+
if (!accessToken) {
|
|
1204
|
+
return json3({ message: "Not signed in." }, 401);
|
|
1205
|
+
}
|
|
1206
|
+
const url = new URL(request2.url);
|
|
1207
|
+
try {
|
|
1208
|
+
const id = url.searchParams.get("id");
|
|
1209
|
+
if (id) {
|
|
1210
|
+
return json3({ order: await backendMyOrder(config, accessToken, id) });
|
|
1211
|
+
}
|
|
1212
|
+
const skip = Math.max(
|
|
1213
|
+
0,
|
|
1214
|
+
Math.floor(Number(url.searchParams.get("skip")) || 0)
|
|
1215
|
+
);
|
|
1216
|
+
const limitParam = Math.floor(Number(url.searchParams.get("limit")));
|
|
1217
|
+
const limit = Number.isFinite(limitParam) && limitParam > 0 ? Math.min(limitParam, MAX_LIMIT) : DEFAULT_LIMIT;
|
|
1218
|
+
const result = await backendMyOrders(config, accessToken, {
|
|
1219
|
+
skip,
|
|
1220
|
+
limit
|
|
1221
|
+
});
|
|
1222
|
+
return json3(result);
|
|
1223
|
+
} catch (err) {
|
|
1224
|
+
return json3(
|
|
1225
|
+
{ message: err instanceof Error ? err.message : "Orders error" },
|
|
1226
|
+
502
|
|
1227
|
+
);
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
};
|
|
1231
|
+
}
|
|
1079
1232
|
|
|
1080
|
-
export { CMSSY_CART_COOKIE, CMSSY_EDIT_HEADER, CMSSY_LOCALE_HEADER, CMSSY_SESSION_COOKIE, SESSION_MAX_AGE_SECONDS, applyCmssyCsp, assertAuthConfig, cmssyCspHeaders, createCmssyAuthMiddleware, createCmssyAuthRoute, createCmssyCartRoute, createCmssyPage, createDraftRoute, getCmssyAccessToken, getCmssyLocale, getCmssyUser, isAccessExpired, isCmssyEditMode, isCmssyEditRequest, localeForPathname, openSession, sealSession, sessionCookieOptions, splitCmssyLocale };
|
|
1233
|
+
export { CMSSY_CART_COOKIE, CMSSY_EDIT_HEADER, CMSSY_LOCALE_HEADER, CMSSY_SESSION_COOKIE, SESSION_MAX_AGE_SECONDS, applyCmssyCsp, assertAuthConfig, cmssyCspHeaders, createCmssyAuthMiddleware, createCmssyAuthRoute, createCmssyCartRoute, createCmssyOrdersRoute, createCmssyPage, createDraftRoute, fetchProduct, fetchProducts, getCmssyAccessToken, getCmssyLocale, getCmssyUser, isAccessExpired, isCmssyEditMode, isCmssyEditRequest, localeForPathname, openSession, sealSession, sessionCookieOptions, splitCmssyLocale };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cmssy/next",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Next.js App Router bindings for cmssy headless sites (createCmssyPage + draft preview)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cmssy",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"dist"
|
|
37
37
|
],
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"@cmssy/react": "^0.2.
|
|
39
|
+
"@cmssy/react": "^0.2.3",
|
|
40
40
|
"next": ">=15",
|
|
41
41
|
"react": "^18.2.0 || ^19.0.0",
|
|
42
42
|
"react-dom": "^18.2.0 || ^19.0.0"
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"tsup": "^8.3.0",
|
|
50
50
|
"typescript": "^5.6.0",
|
|
51
51
|
"vitest": "^2.1.0",
|
|
52
|
-
"@cmssy/react": "0.2.
|
|
52
|
+
"@cmssy/react": "0.2.3"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
55
|
"jose": "^6.2.3"
|