@superblocksteam/sdk 2.0.55 → 2.0.56-next.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.
Files changed (54) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/application-build.d.mts.map +1 -1
  3. package/dist/application-build.mjs +0 -2
  4. package/dist/application-build.mjs.map +1 -1
  5. package/dist/cli-replacement/automatic-upgrades.d.ts.map +1 -1
  6. package/dist/cli-replacement/automatic-upgrades.js +0 -4
  7. package/dist/cli-replacement/automatic-upgrades.js.map +1 -1
  8. package/dist/cli-replacement/dev.d.mts.map +1 -1
  9. package/dist/cli-replacement/dev.mjs +1 -0
  10. package/dist/cli-replacement/dev.mjs.map +1 -1
  11. package/dist/dev-utils/dev-server.d.mts.map +1 -1
  12. package/dist/dev-utils/dev-server.mjs +98 -6
  13. package/dist/dev-utils/dev-server.mjs.map +1 -1
  14. package/dist/flag.d.ts +0 -1
  15. package/dist/flag.d.ts.map +1 -1
  16. package/dist/flag.js +0 -3
  17. package/dist/flag.js.map +1 -1
  18. package/dist/types/common.d.ts +0 -1
  19. package/dist/types/common.d.ts.map +1 -1
  20. package/dist/types/common.js.map +1 -1
  21. package/dist/types/scoped-jwt-token-payload.d.ts +10 -0
  22. package/dist/types/scoped-jwt-token-payload.d.ts.map +1 -0
  23. package/dist/types/scoped-jwt-token-payload.js +2 -0
  24. package/dist/types/scoped-jwt-token-payload.js.map +1 -0
  25. package/dist/utils/token-utils.d.mts +4 -0
  26. package/dist/utils/token-utils.d.mts.map +1 -1
  27. package/dist/utils/token-utils.mjs +2 -0
  28. package/dist/utils/token-utils.mjs.map +1 -1
  29. package/dist/vite-plugin-inject-sb-ids-transform.d.mts +16 -2
  30. package/dist/vite-plugin-inject-sb-ids-transform.d.mts.map +1 -1
  31. package/dist/vite-plugin-inject-sb-ids-transform.mjs +2 -31
  32. package/dist/vite-plugin-inject-sb-ids-transform.mjs.map +1 -1
  33. package/dist/vite-plugin-optimize-lucide-imports.d.ts.map +1 -1
  34. package/dist/vite-plugin-optimize-lucide-imports.js +2 -0
  35. package/dist/vite-plugin-optimize-lucide-imports.js.map +1 -1
  36. package/package.json +6 -6
  37. package/src/application-build.mts +0 -2
  38. package/src/cli-replacement/automatic-upgrades.ts +0 -4
  39. package/src/cli-replacement/dev.mts +1 -0
  40. package/src/dev-utils/dev-server.mts +118 -7
  41. package/src/flag.ts +0 -4
  42. package/src/types/common.ts +0 -1
  43. package/src/types/scoped-jwt-token-payload.ts +12 -0
  44. package/src/utils/token-utils.mts +6 -0
  45. package/src/vite-plugin-inject-sb-ids-transform.mts +4 -45
  46. package/src/vite-plugin-optimize-lucide-imports.ts +2 -0
  47. package/tsconfig.json +2 -2
  48. package/tsconfig.tsbuildinfo +1 -1
  49. package/turbo.json +2 -1
  50. package/dist/vite-plugin-generate-build-manifest.d.mts +0 -21
  51. package/dist/vite-plugin-generate-build-manifest.d.mts.map +0 -1
  52. package/dist/vite-plugin-generate-build-manifest.mjs +0 -137
  53. package/dist/vite-plugin-generate-build-manifest.mjs.map +0 -1
  54. package/src/vite-plugin-generate-build-manifest.mts +0 -202
package/turbo.json CHANGED
@@ -14,7 +14,8 @@
14
14
  "SUPERBLOCKS_PROFILE",
15
15
  "SUPERBLOCKS_CLARK_CONTEXT_CONFIG",
16
16
  "SUPERBLOCKS_PUBLIC_INTEGRATIONS_CDN_URL",
17
- "SABS_LIVE_EDIT_HOSTNAME"
17
+ "SABS_LIVE_EDIT_HOSTNAME",
18
+ "SUPERBLOCKS_BASE_URL"
18
19
  ]
19
20
  }
20
21
  }
@@ -1,21 +0,0 @@
1
- import type { Plugin } from "vite";
2
- export declare const scopeFileBaseName = "scope.ts";
3
- export declare const apiFileBaseName = "api.yaml";
4
- /**
5
- * Vite plugin that generates a build manifest for the application.
6
- *
7
- * This plugin will:
8
- * 1. Read all api.yaml and api.yml files in the application
9
- * 2. Read all scope.ts files in the application
10
- * 3. Generate a build manifest for the application
11
- * 4. Write the build manifest to a file in the build output directory
12
- *
13
- * The build manifest is a JSON object with the following properties:
14
- * - apis: a map of all apis in the application keyed by their file path
15
- * - apiDependencies: an array where each item represents an api and its dependencies
16
- *
17
- * @param root - The root directory of the application
18
- * @returns A Vite plugin that generates a build manifest for the application
19
- */
20
- export declare function generateBuildManifestPlugin(root: string): Plugin;
21
- //# sourceMappingURL=vite-plugin-generate-build-manifest.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"vite-plugin-generate-build-manifest.d.mts","sourceRoot":"","sources":["../src/vite-plugin-generate-build-manifest.mts"],"names":[],"mappings":"AAyBA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAEnC,eAAO,MAAM,iBAAiB,aAAa,CAAC;AAC5C,eAAO,MAAM,eAAe,aAAa,CAAC;AAS1C;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,2BAA2B,CAAC,IAAI,EAAE,MAAM,GAgHjD,MAAM,CACZ"}
@@ -1,137 +0,0 @@
1
- import path from "node:path";
2
- import { parse } from "@babel/parser";
3
- import { getClientApiId } from "@superblocksteam/library-shared";
4
- import { resolveLanguageSpecificStepContentFromBlocks } from "@superblocksteam/util";
5
- import { getPageName } from "@superblocksteam/vite-plugin-file-sync";
6
- import { extractIdentifierPathsFromApi, extractApiDependencies, } from "@superblocksteam/vite-plugin-file-sync/binding-extraction";
7
- import { getScope, extractImportsFromAst, } from "@superblocksteam/vite-plugin-file-sync/parsing";
8
- import { yellow, red } from "colorette";
9
- import fg from "fast-glob";
10
- import fs from "fs-extra";
11
- import { createLogger } from "vite";
12
- import yaml from "yaml";
13
- import { getLogger } from "./telemetry/logging.js";
14
- export const scopeFileBaseName = "scope.ts";
15
- export const apiFileBaseName = "api.yaml";
16
- /**
17
- * Vite plugin that generates a build manifest for the application.
18
- *
19
- * This plugin will:
20
- * 1. Read all api.yaml and api.yml files in the application
21
- * 2. Read all scope.ts files in the application
22
- * 3. Generate a build manifest for the application
23
- * 4. Write the build manifest to a file in the build output directory
24
- *
25
- * The build manifest is a JSON object with the following properties:
26
- * - apis: a map of all apis in the application keyed by their file path
27
- * - apiDependencies: an array where each item represents an api and its dependencies
28
- *
29
- * @param root - The root directory of the application
30
- * @returns A Vite plugin that generates a build manifest for the application
31
- */
32
- export function generateBuildManifestPlugin(root) {
33
- const viteLogger = createLogger();
34
- const logger = getLogger();
35
- viteLogger.info = (msg) => logger.info(msg);
36
- viteLogger.warn = (msg) => {
37
- logger.warn(yellow(msg));
38
- };
39
- viteLogger.warnOnce = (msg) => {
40
- logger.warn(yellow(msg));
41
- };
42
- viteLogger.error = (msg) => {
43
- logger.error(red(msg));
44
- };
45
- viteLogger.clearScreen = () => { };
46
- const scopesByPage = {};
47
- const apiFiles = {};
48
- let buildManifest = { apis: {}, apiDependencies: [] };
49
- return {
50
- name: "sb-generate-build-manifest",
51
- apply: "build",
52
- enforce: "pre",
53
- async buildStart() {
54
- const apiDocuments = await fg(["**/api.yaml", "**/api.yml"], {
55
- cwd: root,
56
- });
57
- for (const apiFilePath of apiDocuments) {
58
- const absoluteApiFilePath = path.join(root, apiFilePath);
59
- const document = await fs.readFile(absoluteApiFilePath, "utf-8");
60
- const apiPb = yaml.parse(document);
61
- await resolveLanguageSpecificStepContentFromBlocks(path.dirname(absoluteApiFilePath), apiPb.blocks ?? [], {});
62
- const pageName = getPageName(apiFilePath);
63
- apiPb.metadata.id = getClientApiId(apiPb.metadata.name, pageName);
64
- apiFiles[apiFilePath] = { apiPb, pageName };
65
- }
66
- },
67
- transform(code, id) {
68
- if (id.endsWith(scopeFileBaseName)) {
69
- const ast = parse(code, {
70
- sourceType: "module",
71
- sourceFilename: id,
72
- plugins: [["typescript", {}]],
73
- });
74
- const scopeFileMeta = {
75
- code,
76
- ast,
77
- imports: extractImportsFromAst(ast),
78
- };
79
- const scope = getScope(id, scopeFileMeta);
80
- scopesByPage[getPageName(id)] = scope;
81
- }
82
- return null;
83
- },
84
- async generateBundle(_options, bundle) {
85
- const allApiNames = getAllUniqueApiNames(apiFiles);
86
- const allApis = Object.entries(apiFiles).reduce((acc, [id, api]) => {
87
- acc[id] = {
88
- api,
89
- scopeId: scopesByPage[api.pageName]?.scopeId ?? "",
90
- };
91
- return acc;
92
- }, {});
93
- const deps = await getApiDependencies(allApiNames, allApis);
94
- buildManifest = {
95
- apis: allApis,
96
- apiDependencies: deps,
97
- };
98
- // Find the existing hashed build-manifest file in the bundle and replace its content
99
- for (const [fileName, chunk] of Object.entries(bundle)) {
100
- // Look for build-manifest files specifically in the assets folder
101
- if (fileName.startsWith("assets/build-manifest") &&
102
- fileName.endsWith(".js")) {
103
- // This is the hashed build-manifest file, replace its content
104
- if (chunk.type === "chunk") {
105
- chunk.code =
106
- `export default ${JSON.stringify(buildManifest)};`;
107
- }
108
- }
109
- }
110
- },
111
- };
112
- }
113
- function getAllUniqueApiNames(apiFiles) {
114
- const allApiNamesSet = new Set(Object.values(apiFiles).map((api) => api.apiPb.metadata.name));
115
- return Array.from(allApiNamesSet);
116
- }
117
- async function getApiDependencies(apiNames, apiFiles) {
118
- const deps = [];
119
- // TODO: This logic is a copy of the extractApiParamsAndDependencies function in the
120
- // fileSyncVitePlugin (https://github.com/superblocksteam/superblocks/blob/474c0b11d79a1d6988864fb20d5ec26f92931fde/packages/vite-plugin-file-sync/src/file-sync-vite-plugin.ts#L261-L280).
121
- //
122
- // This logic should be refactored so that it can be shared between the two plugins.
123
- await Promise.all([
124
- ...Object.values(apiFiles).map(async ({ api, scopeId }) => {
125
- const bindings = await extractIdentifierPathsFromApi(api);
126
- const apiDependencies = extractApiDependencies(apiNames, bindings);
127
- deps.push({
128
- apiName: api.apiPb.metadata.name,
129
- scopeId,
130
- params: bindings,
131
- dependencies: apiDependencies,
132
- });
133
- }),
134
- ]);
135
- return deps;
136
- }
137
- //# sourceMappingURL=vite-plugin-generate-build-manifest.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"vite-plugin-generate-build-manifest.mjs","sourceRoot":"","sources":["../src/v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CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;YACnE,IAAI,CAAC,IAAI,CAAC;gBACR,OAAO,EAAE,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI;gBAChC,OAAO;gBACP,MAAM,EAAE,QAAQ;gBAChB,YAAY,EAAE,eAAe;aAC9B,CAAC,CAAC;QACL,CAAC,CAAC;KACH,CAAC,CAAC;IAEH,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -1,202 +0,0 @@
1
- import path from "node:path";
2
- import { parse } from "@babel/parser";
3
- import { getClientApiId } from "@superblocksteam/library-shared";
4
- import { resolveLanguageSpecificStepContentFromBlocks } from "@superblocksteam/util";
5
- import { getPageName } from "@superblocksteam/vite-plugin-file-sync";
6
- import {
7
- extractIdentifierPathsFromApi,
8
- extractApiDependencies,
9
- } from "@superblocksteam/vite-plugin-file-sync/binding-extraction";
10
- import {
11
- getScope,
12
- extractImportsFromAst,
13
- } from "@superblocksteam/vite-plugin-file-sync/parsing";
14
- import { yellow, red } from "colorette";
15
- import fg from "fast-glob";
16
- import fs from "fs-extra";
17
- import { createLogger } from "vite";
18
- import yaml from "yaml";
19
- import { getLogger } from "./telemetry/logging.js";
20
- import type { ParseResult } from "@babel/parser";
21
- import type { File } from "@babel/types";
22
- import type {
23
- DeleteMeLibraryApi,
24
- StaticScope,
25
- } from "@superblocksteam/library-shared/types";
26
- import type { Plugin } from "vite";
27
-
28
- export const scopeFileBaseName = "scope.ts";
29
- export const apiFileBaseName = "api.yaml";
30
-
31
- type ApiDependency = {
32
- apiName: string;
33
- scopeId: string;
34
- params: string[];
35
- dependencies: string[];
36
- };
37
-
38
- /**
39
- * Vite plugin that generates a build manifest for the application.
40
- *
41
- * This plugin will:
42
- * 1. Read all api.yaml and api.yml files in the application
43
- * 2. Read all scope.ts files in the application
44
- * 3. Generate a build manifest for the application
45
- * 4. Write the build manifest to a file in the build output directory
46
- *
47
- * The build manifest is a JSON object with the following properties:
48
- * - apis: a map of all apis in the application keyed by their file path
49
- * - apiDependencies: an array where each item represents an api and its dependencies
50
- *
51
- * @param root - The root directory of the application
52
- * @returns A Vite plugin that generates a build manifest for the application
53
- */
54
- export function generateBuildManifestPlugin(root: string) {
55
- const viteLogger = createLogger();
56
- const logger = getLogger();
57
- viteLogger.info = (msg: string) => logger.info(msg);
58
- viteLogger.warn = (msg: string) => {
59
- logger.warn(yellow(msg));
60
- };
61
- viteLogger.warnOnce = (msg: string) => {
62
- logger.warn(yellow(msg));
63
- };
64
- viteLogger.error = (msg: string) => {
65
- logger.error(red(msg));
66
- };
67
-
68
- viteLogger.clearScreen = () => {};
69
-
70
- const scopesByPage: Record<string, StaticScope | null> = {};
71
- const apiFiles: Record<string, DeleteMeLibraryApi> = {};
72
- let buildManifest: {
73
- apis: Record<string, { api: DeleteMeLibraryApi; scopeId: string }>;
74
- apiDependencies: ApiDependency[];
75
- } = { apis: {}, apiDependencies: [] };
76
-
77
- return {
78
- name: "sb-generate-build-manifest",
79
- apply: "build",
80
- enforce: "pre",
81
-
82
- async buildStart() {
83
- const apiDocuments = await fg(["**/api.yaml", "**/api.yml"], {
84
- cwd: root,
85
- });
86
-
87
- for (const apiFilePath of apiDocuments) {
88
- const absoluteApiFilePath = path.join(root, apiFilePath);
89
-
90
- const document = await fs.readFile(absoluteApiFilePath, "utf-8");
91
- const apiPb = yaml.parse(document);
92
-
93
- await resolveLanguageSpecificStepContentFromBlocks(
94
- path.dirname(absoluteApiFilePath),
95
- apiPb.blocks ?? [],
96
- {},
97
- );
98
-
99
- const pageName = getPageName(apiFilePath);
100
- apiPb.metadata.id = getClientApiId(apiPb.metadata.name, pageName);
101
- apiFiles[apiFilePath] = { apiPb, pageName };
102
- }
103
- },
104
-
105
- transform(code: string, id: string) {
106
- if (id.endsWith(scopeFileBaseName)) {
107
- const ast = parse(code, {
108
- sourceType: "module",
109
- sourceFilename: id,
110
- plugins: [["typescript", {}]],
111
- });
112
-
113
- const scopeFileMeta: {
114
- code: string;
115
- ast: ParseResult<File>;
116
- imports: Record<string, string>;
117
- } = {
118
- code,
119
- ast,
120
- imports: extractImportsFromAst(ast),
121
- };
122
- const scope = getScope(id, scopeFileMeta);
123
-
124
- scopesByPage[getPageName(id)] = scope;
125
- }
126
-
127
- return null;
128
- },
129
-
130
- async generateBundle(_options: any, bundle: any) {
131
- const allApiNames = getAllUniqueApiNames(apiFiles);
132
-
133
- const allApis = Object.entries(apiFiles).reduce(
134
- (acc, [id, api]) => {
135
- acc[id] = {
136
- api,
137
- scopeId: scopesByPage[api.pageName]?.scopeId ?? "",
138
- };
139
- return acc;
140
- },
141
- {} as Record<string, { api: DeleteMeLibraryApi; scopeId: string }>,
142
- );
143
-
144
- const deps = await getApiDependencies(allApiNames, allApis);
145
-
146
- buildManifest = {
147
- apis: allApis,
148
- apiDependencies: deps,
149
- };
150
-
151
- // Find the existing hashed build-manifest file in the bundle and replace its content
152
- for (const [fileName, chunk] of Object.entries(bundle)) {
153
- // Look for build-manifest files specifically in the assets folder
154
- if (
155
- fileName.startsWith("assets/build-manifest") &&
156
- fileName.endsWith(".js")
157
- ) {
158
- // This is the hashed build-manifest file, replace its content
159
- if ((chunk as any).type === "chunk") {
160
- (chunk as any).code =
161
- `export default ${JSON.stringify(buildManifest)};`;
162
- }
163
- }
164
- }
165
- },
166
- } as Plugin;
167
- }
168
-
169
- function getAllUniqueApiNames(
170
- apiFiles: Record<string, DeleteMeLibraryApi>,
171
- ): string[] {
172
- const allApiNamesSet = new Set<string>(
173
- Object.values(apiFiles).map((api) => api.apiPb.metadata.name),
174
- );
175
- return Array.from(allApiNamesSet);
176
- }
177
-
178
- async function getApiDependencies(
179
- apiNames: string[],
180
- apiFiles: Record<string, { api: DeleteMeLibraryApi; scopeId: string }>,
181
- ): Promise<ApiDependency[]> {
182
- const deps: ApiDependency[] = [];
183
-
184
- // TODO: This logic is a copy of the extractApiParamsAndDependencies function in the
185
- // fileSyncVitePlugin (https://github.com/superblocksteam/superblocks/blob/474c0b11d79a1d6988864fb20d5ec26f92931fde/packages/vite-plugin-file-sync/src/file-sync-vite-plugin.ts#L261-L280).
186
- //
187
- // This logic should be refactored so that it can be shared between the two plugins.
188
- await Promise.all([
189
- ...Object.values(apiFiles).map(async ({ api, scopeId }) => {
190
- const bindings = await extractIdentifierPathsFromApi(api);
191
- const apiDependencies = extractApiDependencies(apiNames, bindings);
192
- deps.push({
193
- apiName: api.apiPb.metadata.name,
194
- scopeId,
195
- params: bindings,
196
- dependencies: apiDependencies,
197
- });
198
- }),
199
- ]);
200
-
201
- return deps;
202
- }