@uniformdev/canvas-next 18.5.0 → 18.6.0

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,14 @@
1
+ // src/helpers/slug.ts
2
+ var resolveSlugFromParams = ({
3
+ param = "slug",
4
+ params,
5
+ prefix
6
+ }) => {
7
+ const slug = (params == null ? void 0 : params[param]) || "";
8
+ const slugString = Array.isArray(slug) ? slug.join("/") : slug;
9
+ return `/${slugString}` || prefix || "/";
10
+ };
11
+
12
+ export {
13
+ resolveSlugFromParams
14
+ };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,14 @@
1
- export { U as UniformCompositionNextPage, b as UniformGetServerSideProps, a as UniformGetStaticProps } from './models-ed92e2bd.js';
1
+ import { ParsedUrlQuery } from 'querystring';
2
+ export { U as UniformCompositionNextPage, c as UniformGetServerSideProps, b as UniformGetStaticProps, a as UniformPreviewData } from './models-6ff72d91.js';
2
3
  import { NextApiHandler } from 'next';
3
4
  import { RootComponentInstance } from '@uniformdev/canvas';
4
5
  import '@uniformdev/canvas-react';
5
- import 'querystring';
6
+
7
+ declare const resolveSlugFromParams: ({ param, params, prefix, }: {
8
+ param: string | undefined;
9
+ params: ParsedUrlQuery | undefined;
10
+ prefix?: string | undefined;
11
+ }) => string;
6
12
 
7
13
  type ResolveFullPath = (options: {
8
14
  id?: string;
@@ -30,4 +36,4 @@ declare const createPreviewHandlerPost: CreatePreviewHandlerPost;
30
36
  type CreatePreviewHandler = (options?: CreatePreviewHandlerGetOptions & CreatePreviewHandlerPostOptions) => NextApiHandler;
31
37
  declare const createPreviewHandler: CreatePreviewHandler;
32
38
 
33
- export { CreatePreviewHandler, CreatePreviewHandlerGet, CreatePreviewHandlerGetOptions, CreatePreviewHandlerPost, CreatePreviewHandlerPostOptions, ResolveFullPath, createPreviewHandler, createPreviewHandlerGet, createPreviewHandlerPost };
39
+ export { CreatePreviewHandler, CreatePreviewHandlerGet, CreatePreviewHandlerGetOptions, CreatePreviewHandlerPost, CreatePreviewHandlerPostOptions, ResolveFullPath, createPreviewHandler, createPreviewHandlerGet, createPreviewHandlerPost, resolveSlugFromParams };
package/dist/index.esm.js CHANGED
@@ -1,3 +1,7 @@
1
+ import {
2
+ resolveSlugFromParams
3
+ } from "./chunk-SH3D2DYY.mjs";
4
+
1
5
  // src/preview/previewHandlerGet.ts
2
6
  import { IN_CONTEXT_EDITOR_QUERY_STRING_PARAM } from "@uniformdev/canvas";
3
7
  var createPreviewHandlerGet = ({ secret, resolveFullPath = resolveFullPathDefault } = {}) => async (req, res) => {
@@ -19,7 +23,9 @@ var createPreviewHandlerGet = ({ secret, resolveFullPath = resolveFullPathDefaul
19
23
  if (isUsingPreviewSecret && req.query.secret !== previewSecret) {
20
24
  return res.status(401).json({ message: "Invalid token" });
21
25
  }
22
- res.setPreviewData({});
26
+ res.setPreviewData({
27
+ isUniformContextualEditing: req.query[IN_CONTEXT_EDITOR_QUERY_STRING_PARAM] === "true"
28
+ });
23
29
  const newQuery = new URLSearchParams();
24
30
  queryParamsToPreserve.forEach((param) => {
25
31
  const paramValue = req.query[param];
@@ -76,5 +82,6 @@ var createPreviewHandler = ({ secret, enhance, resolveFullPath } = {}) => async
76
82
  export {
77
83
  createPreviewHandler,
78
84
  createPreviewHandlerGet,
79
- createPreviewHandlerPost
85
+ createPreviewHandlerPost,
86
+ resolveSlugFromParams
80
87
  };
package/dist/index.js CHANGED
@@ -22,10 +22,22 @@ var src_exports = {};
22
22
  __export(src_exports, {
23
23
  createPreviewHandler: () => createPreviewHandler,
24
24
  createPreviewHandlerGet: () => createPreviewHandlerGet,
25
- createPreviewHandlerPost: () => createPreviewHandlerPost
25
+ createPreviewHandlerPost: () => createPreviewHandlerPost,
26
+ resolveSlugFromParams: () => resolveSlugFromParams
26
27
  });
27
28
  module.exports = __toCommonJS(src_exports);
28
29
 
30
+ // src/helpers/slug.ts
31
+ var resolveSlugFromParams = ({
32
+ param = "slug",
33
+ params,
34
+ prefix
35
+ }) => {
36
+ const slug = (params == null ? void 0 : params[param]) || "";
37
+ const slugString = Array.isArray(slug) ? slug.join("/") : slug;
38
+ return `/${slugString}` || prefix || "/";
39
+ };
40
+
29
41
  // src/preview/previewHandlerGet.ts
30
42
  var import_canvas = require("@uniformdev/canvas");
31
43
  var createPreviewHandlerGet = ({ secret, resolveFullPath = resolveFullPathDefault } = {}) => async (req, res) => {
@@ -47,7 +59,9 @@ var createPreviewHandlerGet = ({ secret, resolveFullPath = resolveFullPathDefaul
47
59
  if (isUsingPreviewSecret && req.query.secret !== previewSecret) {
48
60
  return res.status(401).json({ message: "Invalid token" });
49
61
  }
50
- res.setPreviewData({});
62
+ res.setPreviewData({
63
+ isUniformContextualEditing: req.query[import_canvas.IN_CONTEXT_EDITOR_QUERY_STRING_PARAM] === "true"
64
+ });
51
65
  const newQuery = new URLSearchParams();
52
66
  queryParamsToPreserve.forEach((param) => {
53
67
  const paramValue = req.query[param];
@@ -105,5 +119,6 @@ var createPreviewHandler = ({ secret, enhance, resolveFullPath } = {}) => async
105
119
  0 && (module.exports = {
106
120
  createPreviewHandler,
107
121
  createPreviewHandlerGet,
108
- createPreviewHandlerPost
122
+ createPreviewHandlerPost,
123
+ resolveSlugFromParams
109
124
  });
package/dist/index.mjs CHANGED
@@ -1,3 +1,7 @@
1
+ import {
2
+ resolveSlugFromParams
3
+ } from "./chunk-SH3D2DYY.mjs";
4
+
1
5
  // src/preview/previewHandlerGet.ts
2
6
  import { IN_CONTEXT_EDITOR_QUERY_STRING_PARAM } from "@uniformdev/canvas";
3
7
  var createPreviewHandlerGet = ({ secret, resolveFullPath = resolveFullPathDefault } = {}) => async (req, res) => {
@@ -19,7 +23,9 @@ var createPreviewHandlerGet = ({ secret, resolveFullPath = resolveFullPathDefaul
19
23
  if (isUsingPreviewSecret && req.query.secret !== previewSecret) {
20
24
  return res.status(401).json({ message: "Invalid token" });
21
25
  }
22
- res.setPreviewData({});
26
+ res.setPreviewData({
27
+ isUniformContextualEditing: req.query[IN_CONTEXT_EDITOR_QUERY_STRING_PARAM] === "true"
28
+ });
23
29
  const newQuery = new URLSearchParams();
24
30
  queryParamsToPreserve.forEach((param) => {
25
31
  const paramValue = req.query[param];
@@ -76,5 +82,6 @@ var createPreviewHandler = ({ secret, enhance, resolveFullPath } = {}) => async
76
82
  export {
77
83
  createPreviewHandler,
78
84
  createPreviewHandlerGet,
79
- createPreviewHandlerPost
85
+ createPreviewHandlerPost,
86
+ resolveSlugFromParams
80
87
  };
@@ -1,18 +1,21 @@
1
1
  import { RootComponentInstance } from '@uniformdev/canvas';
2
- import { CompositionProps } from '@uniformdev/canvas-react';
2
+ import { UniformCompositionProps } from '@uniformdev/canvas-react';
3
3
  import { NextPage, PreviewData, GetStaticPropsContext, GetStaticPropsResult, GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
4
4
  import { ParsedUrlQuery } from 'querystring';
5
5
 
6
- type UniformCompositionNextPage<TProps = unknown> = NextPage<TProps & CompositionProps>;
6
+ type UniformCompositionNextPage<TProps = unknown> = NextPage<TProps & UniformCompositionProps>;
7
+ type UniformPreviewData = PreviewData & {
8
+ isUniformContextualEditing: boolean;
9
+ };
7
10
  type UniformGetStaticProps<P extends {
8
11
  [key: string]: any;
9
12
  } = {
10
13
  [key: string]: any;
11
- }, Q extends ParsedUrlQuery = ParsedUrlQuery, D extends PreviewData = PreviewData> = (context: GetStaticPropsContext<Q, D>, composition: RootComponentInstance | undefined) => Promise<GetStaticPropsResult<P>> | GetStaticPropsResult<P>;
14
+ }, Q extends ParsedUrlQuery = ParsedUrlQuery, D extends UniformPreviewData = UniformPreviewData> = (context: GetStaticPropsContext<Q, D>, composition: RootComponentInstance | undefined) => Promise<GetStaticPropsResult<P>> | GetStaticPropsResult<P>;
12
15
  type UniformGetServerSideProps<P extends {
13
16
  [key: string]: any;
14
17
  } = {
15
18
  [key: string]: any;
16
19
  }, Q extends ParsedUrlQuery = ParsedUrlQuery, D extends PreviewData = PreviewData> = (context: GetServerSidePropsContext<Q, D>, composition: RootComponentInstance | undefined) => Promise<GetServerSidePropsResult<P>> | GetServerSidePropsResult<P>;
17
20
 
18
- export { UniformCompositionNextPage as U, UniformGetStaticProps as a, UniformGetServerSideProps as b };
21
+ export { UniformCompositionNextPage as U, UniformPreviewData as a, UniformGetStaticProps as b, UniformGetServerSideProps as c };
@@ -1,8 +1,9 @@
1
+ import * as querystring from 'querystring';
2
+ import { ParsedUrlQuery } from 'querystring';
3
+ import { c as UniformGetServerSideProps, b as UniformGetStaticProps, a as UniformPreviewData } from '../models-6ff72d91.js';
1
4
  import * as next from 'next';
2
5
  import { GetServerSideProps, GetStaticProps } from 'next';
3
- import * as querystring from 'querystring';
4
6
  import { CanvasClient } from '@uniformdev/canvas';
5
- import { b as UniformGetServerSideProps, a as UniformGetStaticProps } from '../models-ed92e2bd.js';
6
7
  import { ProjectMapClient } from '@uniformdev/project-map';
7
8
  import '@uniformdev/canvas-react';
8
9
 
@@ -30,19 +31,19 @@ declare const withUniformGetStaticProps: <TProps extends {
30
31
  [key: string]: any;
31
32
  } = {
32
33
  [key: string]: any;
33
- }>(callback?: UniformGetStaticProps<TProps, querystring.ParsedUrlQuery, next.PreviewData> | undefined, options?: {
34
+ }>(callback?: UniformGetStaticProps<TProps, ParsedUrlQuery, UniformPreviewData> | undefined, options?: {
34
35
  projectMapId?: string;
35
36
  param?: string;
36
37
  prefix?: string;
37
38
  client?: CanvasClient;
38
- }) => GetStaticProps<TProps, querystring.ParsedUrlQuery, next.PreviewData>;
39
+ }) => GetStaticProps<TProps, ParsedUrlQuery, UniformPreviewData>;
39
40
 
40
41
  declare const getServerSideProps: next.GetServerSideProps<{
41
42
  [key: string]: any;
42
43
  }, querystring.ParsedUrlQuery, next.PreviewData>;
43
44
  declare const getStaticProps: next.GetStaticProps<{
44
45
  [key: string]: any;
45
- }, querystring.ParsedUrlQuery, next.PreviewData>;
46
+ }, querystring.ParsedUrlQuery, UniformPreviewData>;
46
47
  declare const getStaticPaths: () => Promise<{
47
48
  paths: string[] | undefined;
48
49
  fallback: boolean;
@@ -112,10 +112,9 @@ var withUniformGetStaticPaths = (options) => {
112
112
  };
113
113
 
114
114
  // src/project-map/withUniformGetStaticProps.ts
115
- var import_canvas4 = require("@uniformdev/canvas");
116
-
117
- // src/slug/withUniformGetStaticProps.ts
118
115
  var import_canvas3 = require("@uniformdev/canvas");
116
+
117
+ // src/helpers/slug.ts
119
118
  var resolveSlugFromParams = ({
120
119
  param = "slug",
121
120
  params,
@@ -147,23 +146,28 @@ var withUniformGetStaticProps = (callback, options) => {
147
146
  params: context == null ? void 0 : context.params,
148
147
  prefix: options == null ? void 0 : options.prefix
149
148
  });
150
- const canvasClient = (_c = options == null ? void 0 : options.client) != null ? _c : new import_canvas4.CanvasClient({
149
+ const canvasClient = (_c = options == null ? void 0 : options.client) != null ? _c : new import_canvas3.CanvasClient({
151
150
  apiKey: process.env.UNIFORM_API_KEY,
152
151
  projectId: process.env.UNIFORM_PROJECT_ID,
153
152
  apiHost: process.env.UNIFORM_CLI_BASE_URL
154
153
  });
155
- const { preview } = context;
156
- const response = await canvasClient.unstable_getCompositionByNodePath({
157
- projectMapId,
158
- projectMapNodePath: pathString,
159
- state: preview ? import_canvas4.CANVAS_DRAFT_STATE : import_canvas4.CANVAS_PUBLISHED_STATE
160
- });
161
- if (!response.composition) {
162
- return {
163
- notFound: true
164
- };
154
+ const { preview, previewData } = context;
155
+ let composition = void 0;
156
+ try {
157
+ const response = await canvasClient.unstable_getCompositionByNodePath({
158
+ projectMapId,
159
+ projectMapNodePath: pathString,
160
+ state: preview ? import_canvas3.CANVAS_DRAFT_STATE : import_canvas3.CANVAS_PUBLISHED_STATE
161
+ });
162
+ composition = response.composition;
163
+ } catch (e) {
164
+ console.error("[canvas-next] Failed to fetch composition", e);
165
+ if (!(previewData == null ? void 0 : previewData.isUniformContextualEditing)) {
166
+ return {
167
+ notFound: true
168
+ };
169
+ }
165
170
  }
166
- const { composition } = response;
167
171
  const ret = callback ? await callback(context, composition) : { props: {} };
168
172
  if (Object.hasOwn(ret, "props")) {
169
173
  const casted = ret;
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  resolveSlugFromParams
3
- } from "../chunk-NKFDQUQK.mjs";
3
+ } from "../chunk-SH3D2DYY.mjs";
4
4
 
5
5
  // src/project-map/withUniformGetServerSideProps.ts
6
6
  import { CANVAS_DRAFT_STATE, CANVAS_PUBLISHED_STATE, CanvasClient } from "@uniformdev/canvas";
@@ -85,7 +85,11 @@ var withUniformGetStaticPaths = (options) => {
85
85
  };
86
86
 
87
87
  // src/project-map/withUniformGetStaticProps.ts
88
- import { CANVAS_DRAFT_STATE as CANVAS_DRAFT_STATE3, CANVAS_PUBLISHED_STATE as CANVAS_PUBLISHED_STATE3, CanvasClient as CanvasClient2 } from "@uniformdev/canvas";
88
+ import {
89
+ CANVAS_DRAFT_STATE as CANVAS_DRAFT_STATE3,
90
+ CANVAS_PUBLISHED_STATE as CANVAS_PUBLISHED_STATE3,
91
+ CanvasClient as CanvasClient2
92
+ } from "@uniformdev/canvas";
89
93
  var withUniformGetStaticProps = (callback, options) => {
90
94
  return async function wrappedGetStaticProps(context) {
91
95
  var _a, _b, _c;
@@ -111,18 +115,23 @@ var withUniformGetStaticProps = (callback, options) => {
111
115
  projectId: process.env.UNIFORM_PROJECT_ID,
112
116
  apiHost: process.env.UNIFORM_CLI_BASE_URL
113
117
  });
114
- const { preview } = context;
115
- const response = await canvasClient.unstable_getCompositionByNodePath({
116
- projectMapId,
117
- projectMapNodePath: pathString,
118
- state: preview ? CANVAS_DRAFT_STATE3 : CANVAS_PUBLISHED_STATE3
119
- });
120
- if (!response.composition) {
121
- return {
122
- notFound: true
123
- };
118
+ const { preview, previewData } = context;
119
+ let composition = void 0;
120
+ try {
121
+ const response = await canvasClient.unstable_getCompositionByNodePath({
122
+ projectMapId,
123
+ projectMapNodePath: pathString,
124
+ state: preview ? CANVAS_DRAFT_STATE3 : CANVAS_PUBLISHED_STATE3
125
+ });
126
+ composition = response.composition;
127
+ } catch (e) {
128
+ console.error("[canvas-next] Failed to fetch composition", e);
129
+ if (!(previewData == null ? void 0 : previewData.isUniformContextualEditing)) {
130
+ return {
131
+ notFound: true
132
+ };
133
+ }
124
134
  }
125
- const { composition } = response;
126
135
  const ret = callback ? await callback(context, composition) : { props: {} };
127
136
  if (Object.hasOwn(ret, "props")) {
128
137
  const casted = ret;
@@ -2,17 +2,19 @@ import * as next from 'next';
2
2
  import { GetServerSideProps, GetStaticProps } from 'next';
3
3
  import * as querystring from 'querystring';
4
4
  import { ParsedUrlQuery } from 'querystring';
5
+ import { c as UniformGetServerSideProps, a as UniformPreviewData, b as UniformGetStaticProps } from '../models-6ff72d91.js';
5
6
  import { CanvasClient } from '@uniformdev/canvas';
6
- import { b as UniformGetServerSideProps, a as UniformGetStaticProps } from '../models-ed92e2bd.js';
7
7
  import '@uniformdev/canvas-react';
8
8
 
9
9
  declare const withUniformGetServerSideProps: <TProps extends {
10
10
  [key: string]: any;
11
11
  } = {
12
12
  [key: string]: any;
13
- }>(callback?: UniformGetServerSideProps<TProps, querystring.ParsedUrlQuery, next.PreviewData> | undefined, options?: {
13
+ }>(callback?: UniformGetServerSideProps<TProps, ParsedUrlQuery, next.PreviewData> | undefined, options?: {
14
+ param?: string;
15
+ prefix?: string;
14
16
  client?: CanvasClient;
15
- }) => GetServerSideProps<TProps, querystring.ParsedUrlQuery, next.PreviewData>;
17
+ }) => GetServerSideProps<TProps, ParsedUrlQuery, UniformPreviewData>;
16
18
 
17
19
  declare const withUniformGetStaticPaths: (options?: {
18
20
  prefix?: string;
@@ -27,18 +29,18 @@ declare const withUniformGetStaticProps: <TProps extends {
27
29
  [key: string]: any;
28
30
  } = {
29
31
  [key: string]: any;
30
- }>(callback?: UniformGetStaticProps<TProps, ParsedUrlQuery, next.PreviewData> | undefined, options?: {
32
+ }>(callback?: UniformGetStaticProps<TProps, ParsedUrlQuery, UniformPreviewData> | undefined, options?: {
31
33
  param?: string;
32
34
  prefix?: string;
33
35
  client?: CanvasClient;
34
- }) => GetStaticProps<TProps, ParsedUrlQuery, next.PreviewData>;
36
+ }) => GetStaticProps<TProps, ParsedUrlQuery, UniformPreviewData>;
35
37
 
36
38
  declare const getServerSideProps: next.GetServerSideProps<{
37
39
  [key: string]: any;
38
- }, querystring.ParsedUrlQuery, next.PreviewData>;
40
+ }, querystring.ParsedUrlQuery, UniformPreviewData>;
39
41
  declare const getStaticProps: next.GetStaticProps<{
40
42
  [key: string]: any;
41
- }, querystring.ParsedUrlQuery, next.PreviewData>;
43
+ }, querystring.ParsedUrlQuery, UniformPreviewData>;
42
44
  declare const getStaticPaths: () => Promise<{
43
45
  paths: string[];
44
46
  fallback: boolean;
@@ -31,6 +31,19 @@ module.exports = __toCommonJS(slug_exports);
31
31
 
32
32
  // src/slug/withUniformGetServerSideProps.ts
33
33
  var import_canvas = require("@uniformdev/canvas");
34
+
35
+ // src/helpers/slug.ts
36
+ var resolveSlugFromParams = ({
37
+ param = "slug",
38
+ params,
39
+ prefix
40
+ }) => {
41
+ const slug = (params == null ? void 0 : params[param]) || "";
42
+ const slugString = Array.isArray(slug) ? slug.join("/") : slug;
43
+ return `/${slugString}` || prefix || "/";
44
+ };
45
+
46
+ // src/slug/withUniformGetServerSideProps.ts
34
47
  var withUniformGetServerSideProps = (callback, options) => {
35
48
  return async function wrappedGetServerSideProps(context) {
36
49
  if (!process.env.UNIFORM_API_KEY) {
@@ -39,26 +52,36 @@ var withUniformGetServerSideProps = (callback, options) => {
39
52
  if (!process.env.UNIFORM_PROJECT_ID) {
40
53
  throw new Error("UNIFORM_PROJECT_ID is not defined.");
41
54
  }
55
+ const slugString = resolveSlugFromParams({
56
+ param: options == null ? void 0 : options.param,
57
+ params: context == null ? void 0 : context.params,
58
+ prefix: options == null ? void 0 : options.prefix
59
+ });
42
60
  const canvasClient = (options == null ? void 0 : options.client) || new import_canvas.CanvasClient({
43
61
  apiKey: process.env.UNIFORM_API_KEY,
44
62
  projectId: process.env.UNIFORM_PROJECT_ID,
45
63
  apiHost: process.env.UNIFORM_CLI_BASE_URL
46
64
  });
47
- const { preview } = context;
48
- const url = new URL(context.resolvedUrl, `http://${context.req.headers.host}/`);
49
- const response = await canvasClient.getCompositionBySlug({
50
- slug: url.pathname,
51
- state: preview ? import_canvas.CANVAS_DRAFT_STATE : import_canvas.CANVAS_PUBLISHED_STATE
52
- });
53
- if (!response) {
54
- return {
55
- notFound: true
56
- };
65
+ const { preview, previewData } = context;
66
+ let composition = void 0;
67
+ try {
68
+ const response = await canvasClient.getCompositionBySlug({
69
+ slug: slugString,
70
+ state: preview ? import_canvas.CANVAS_DRAFT_STATE : import_canvas.CANVAS_PUBLISHED_STATE
71
+ });
72
+ composition = response.composition;
73
+ } catch (e) {
74
+ console.error("[canvas-next] Failed to fetch composition", e);
75
+ if (!(previewData == null ? void 0 : previewData.isUniformContextualEditing)) {
76
+ return {
77
+ notFound: true
78
+ };
79
+ }
57
80
  }
58
- const ret = callback ? await callback(context, response.composition) : { props: {} };
59
- if (Object.hasOwn(ret, "props")) {
81
+ const ret = callback ? await callback(context, composition) : { props: {} };
82
+ if (composition && Object.hasOwn(ret, "props")) {
60
83
  const casted = ret;
61
- casted.props["data"] = response.composition;
84
+ casted.props["data"] = composition;
62
85
  }
63
86
  return ret;
64
87
  };
@@ -117,34 +140,30 @@ var withUniformGetStaticProps = (callback, options) => {
117
140
  projectId: process.env.UNIFORM_PROJECT_ID,
118
141
  apiHost: process.env.UNIFORM_CLI_BASE_URL
119
142
  });
120
- const { preview } = context;
121
- const response = await canvasClient.getCompositionBySlug({
122
- slug: slugString,
123
- state: preview ? import_canvas3.CANVAS_DRAFT_STATE : import_canvas3.CANVAS_PUBLISHED_STATE
124
- });
125
- if (!response.composition) {
126
- return {
127
- notFound: true
128
- };
143
+ const { preview, previewData } = context;
144
+ let composition = void 0;
145
+ try {
146
+ const response = await canvasClient.getCompositionBySlug({
147
+ slug: slugString,
148
+ state: preview ? import_canvas3.CANVAS_DRAFT_STATE : import_canvas3.CANVAS_PUBLISHED_STATE
149
+ });
150
+ composition = response.composition;
151
+ } catch (e) {
152
+ console.error("[canvas-next] Failed to fetch composition", e);
153
+ if (!(previewData == null ? void 0 : previewData.isUniformContextualEditing)) {
154
+ return {
155
+ notFound: true
156
+ };
157
+ }
129
158
  }
130
- const { composition } = response;
131
159
  const ret = callback ? await callback(context, composition) : { props: {} };
132
- if (Object.hasOwn(ret, "props")) {
160
+ if (composition && Object.hasOwn(ret, "props")) {
133
161
  const casted = ret;
134
162
  casted.props["data"] = composition;
135
163
  }
136
164
  return ret;
137
165
  };
138
166
  };
139
- var resolveSlugFromParams = ({
140
- param = "slug",
141
- params,
142
- prefix
143
- }) => {
144
- const slug = (params == null ? void 0 : params[param]) || "";
145
- const slugString = Array.isArray(slug) ? slug.join("/") : slug;
146
- return `/${slugString}` || prefix || "/";
147
- };
148
167
 
149
168
  // src/slug/index.ts
150
169
  var getServerSideProps = withUniformGetServerSideProps();
@@ -1,9 +1,13 @@
1
1
  import {
2
- withUniformGetStaticProps
3
- } from "../chunk-NKFDQUQK.mjs";
2
+ resolveSlugFromParams
3
+ } from "../chunk-SH3D2DYY.mjs";
4
4
 
5
5
  // src/slug/withUniformGetServerSideProps.ts
6
- import { CANVAS_DRAFT_STATE, CANVAS_PUBLISHED_STATE, CanvasClient } from "@uniformdev/canvas";
6
+ import {
7
+ CANVAS_DRAFT_STATE,
8
+ CANVAS_PUBLISHED_STATE,
9
+ CanvasClient
10
+ } from "@uniformdev/canvas";
7
11
  var withUniformGetServerSideProps = (callback, options) => {
8
12
  return async function wrappedGetServerSideProps(context) {
9
13
  if (!process.env.UNIFORM_API_KEY) {
@@ -12,26 +16,36 @@ var withUniformGetServerSideProps = (callback, options) => {
12
16
  if (!process.env.UNIFORM_PROJECT_ID) {
13
17
  throw new Error("UNIFORM_PROJECT_ID is not defined.");
14
18
  }
19
+ const slugString = resolveSlugFromParams({
20
+ param: options == null ? void 0 : options.param,
21
+ params: context == null ? void 0 : context.params,
22
+ prefix: options == null ? void 0 : options.prefix
23
+ });
15
24
  const canvasClient = (options == null ? void 0 : options.client) || new CanvasClient({
16
25
  apiKey: process.env.UNIFORM_API_KEY,
17
26
  projectId: process.env.UNIFORM_PROJECT_ID,
18
27
  apiHost: process.env.UNIFORM_CLI_BASE_URL
19
28
  });
20
- const { preview } = context;
21
- const url = new URL(context.resolvedUrl, `http://${context.req.headers.host}/`);
22
- const response = await canvasClient.getCompositionBySlug({
23
- slug: url.pathname,
24
- state: preview ? CANVAS_DRAFT_STATE : CANVAS_PUBLISHED_STATE
25
- });
26
- if (!response) {
27
- return {
28
- notFound: true
29
- };
29
+ const { preview, previewData } = context;
30
+ let composition = void 0;
31
+ try {
32
+ const response = await canvasClient.getCompositionBySlug({
33
+ slug: slugString,
34
+ state: preview ? CANVAS_DRAFT_STATE : CANVAS_PUBLISHED_STATE
35
+ });
36
+ composition = response.composition;
37
+ } catch (e) {
38
+ console.error("[canvas-next] Failed to fetch composition", e);
39
+ if (!(previewData == null ? void 0 : previewData.isUniformContextualEditing)) {
40
+ return {
41
+ notFound: true
42
+ };
43
+ }
30
44
  }
31
- const ret = callback ? await callback(context, response.composition) : { props: {} };
32
- if (Object.hasOwn(ret, "props")) {
45
+ const ret = callback ? await callback(context, composition) : { props: {} };
46
+ if (composition && Object.hasOwn(ret, "props")) {
33
47
  const casted = ret;
34
- casted.props["data"] = response.composition;
48
+ casted.props["data"] = composition;
35
49
  }
36
50
  return ret;
37
51
  };
@@ -69,6 +83,56 @@ var withUniformGetStaticPaths = (options) => {
69
83
  };
70
84
  };
71
85
 
86
+ // src/slug/withUniformGetStaticProps.ts
87
+ import {
88
+ CANVAS_DRAFT_STATE as CANVAS_DRAFT_STATE3,
89
+ CANVAS_PUBLISHED_STATE as CANVAS_PUBLISHED_STATE3,
90
+ CanvasClient as CanvasClient3
91
+ } from "@uniformdev/canvas";
92
+ var withUniformGetStaticProps = (callback, options) => {
93
+ return async function wrappedGetStaticProps(context) {
94
+ var _a;
95
+ if (!process.env.UNIFORM_API_KEY) {
96
+ throw new Error("UNIFORM_API_KEY is not defined.");
97
+ }
98
+ if (!process.env.UNIFORM_PROJECT_ID) {
99
+ throw new Error("UNIFORM_PROJECT_ID is not defined.");
100
+ }
101
+ const slugString = resolveSlugFromParams({
102
+ param: options == null ? void 0 : options.param,
103
+ params: context == null ? void 0 : context.params,
104
+ prefix: options == null ? void 0 : options.prefix
105
+ });
106
+ const canvasClient = (_a = options == null ? void 0 : options.client) != null ? _a : new CanvasClient3({
107
+ apiKey: process.env.UNIFORM_API_KEY,
108
+ projectId: process.env.UNIFORM_PROJECT_ID,
109
+ apiHost: process.env.UNIFORM_CLI_BASE_URL
110
+ });
111
+ const { preview, previewData } = context;
112
+ let composition = void 0;
113
+ try {
114
+ const response = await canvasClient.getCompositionBySlug({
115
+ slug: slugString,
116
+ state: preview ? CANVAS_DRAFT_STATE3 : CANVAS_PUBLISHED_STATE3
117
+ });
118
+ composition = response.composition;
119
+ } catch (e) {
120
+ console.error("[canvas-next] Failed to fetch composition", e);
121
+ if (!(previewData == null ? void 0 : previewData.isUniformContextualEditing)) {
122
+ return {
123
+ notFound: true
124
+ };
125
+ }
126
+ }
127
+ const ret = callback ? await callback(context, composition) : { props: {} };
128
+ if (composition && Object.hasOwn(ret, "props")) {
129
+ const casted = ret;
130
+ casted.props["data"] = composition;
131
+ }
132
+ return ret;
133
+ };
134
+ };
135
+
72
136
  // src/slug/index.ts
73
137
  var getServerSideProps = withUniformGetServerSideProps();
74
138
  var getStaticProps = withUniformGetStaticProps();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniformdev/canvas-next",
3
- "version": "18.5.0",
3
+ "version": "18.6.0",
4
4
  "description": "Next.js SDK for Uniform Canvas",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "main": "./dist/index.js",
@@ -52,9 +52,9 @@
52
52
  "format": "prettier --write \"src/**/*.{js,ts,tsx}\""
53
53
  },
54
54
  "dependencies": {
55
- "@uniformdev/canvas": "18.5.0",
56
- "@uniformdev/canvas-react": "18.5.0",
57
- "@uniformdev/project-map": "18.5.0"
55
+ "@uniformdev/canvas": "18.6.0",
56
+ "@uniformdev/canvas-react": "18.6.0",
57
+ "@uniformdev/project-map": "18.6.0"
58
58
  },
59
59
  "peerDependencies": {
60
60
  "next": ">= 12.0.0",
@@ -73,5 +73,5 @@
73
73
  "publishConfig": {
74
74
  "access": "public"
75
75
  },
76
- "gitHead": "757923e90bece28162621d867709e08674bdaa05"
76
+ "gitHead": "2e2b4d179180900386f8b3ceb30d4a854a7e22fd"
77
77
  }
@@ -1,54 +0,0 @@
1
- // src/slug/withUniformGetStaticProps.ts
2
- import { CANVAS_DRAFT_STATE, CANVAS_PUBLISHED_STATE, CanvasClient } from "@uniformdev/canvas";
3
- var withUniformGetStaticProps = (callback, options) => {
4
- return async function wrappedGetStaticProps(context) {
5
- var _a;
6
- if (!process.env.UNIFORM_API_KEY) {
7
- throw new Error("UNIFORM_API_KEY is not defined.");
8
- }
9
- if (!process.env.UNIFORM_PROJECT_ID) {
10
- throw new Error("UNIFORM_PROJECT_ID is not defined.");
11
- }
12
- const slugString = resolveSlugFromParams({
13
- param: options == null ? void 0 : options.param,
14
- params: context == null ? void 0 : context.params,
15
- prefix: options == null ? void 0 : options.prefix
16
- });
17
- const canvasClient = (_a = options == null ? void 0 : options.client) != null ? _a : new CanvasClient({
18
- apiKey: process.env.UNIFORM_API_KEY,
19
- projectId: process.env.UNIFORM_PROJECT_ID,
20
- apiHost: process.env.UNIFORM_CLI_BASE_URL
21
- });
22
- const { preview } = context;
23
- const response = await canvasClient.getCompositionBySlug({
24
- slug: slugString,
25
- state: preview ? CANVAS_DRAFT_STATE : CANVAS_PUBLISHED_STATE
26
- });
27
- if (!response.composition) {
28
- return {
29
- notFound: true
30
- };
31
- }
32
- const { composition } = response;
33
- const ret = callback ? await callback(context, composition) : { props: {} };
34
- if (Object.hasOwn(ret, "props")) {
35
- const casted = ret;
36
- casted.props["data"] = composition;
37
- }
38
- return ret;
39
- };
40
- };
41
- var resolveSlugFromParams = ({
42
- param = "slug",
43
- params,
44
- prefix
45
- }) => {
46
- const slug = (params == null ? void 0 : params[param]) || "";
47
- const slugString = Array.isArray(slug) ? slug.join("/") : slug;
48
- return `/${slugString}` || prefix || "/";
49
- };
50
-
51
- export {
52
- withUniformGetStaticProps,
53
- resolveSlugFromParams
54
- };