@plasmicapp/loader-fetcher 1.0.1
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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/api.d.ts +127 -0
- package/dist/fetcher.d.ts +24 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +8 -0
- package/dist/loader-fetcher.cjs.development.js +132 -0
- package/dist/loader-fetcher.cjs.development.js.map +1 -0
- package/dist/loader-fetcher.cjs.production.min.js +2 -0
- package/dist/loader-fetcher.cjs.production.min.js.map +1 -0
- package/dist/loader-fetcher.esm.js +125 -0
- package/dist/loader-fetcher.esm.js.map +1 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Chung Wu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
export interface ComponentMeta {
|
|
2
|
+
id: string;
|
|
3
|
+
usedComponents: string[];
|
|
4
|
+
projectId: string;
|
|
5
|
+
name: string;
|
|
6
|
+
displayName: string;
|
|
7
|
+
cssFile: string;
|
|
8
|
+
path: string | undefined;
|
|
9
|
+
isPage: boolean;
|
|
10
|
+
plumeType?: string;
|
|
11
|
+
entry: string;
|
|
12
|
+
isCode: boolean;
|
|
13
|
+
pageMetadata?: PageMetadata;
|
|
14
|
+
metadata?: Record<string, string>;
|
|
15
|
+
}
|
|
16
|
+
export interface PageMeta extends ComponentMeta {
|
|
17
|
+
isPage: true;
|
|
18
|
+
path: string;
|
|
19
|
+
plumeType: never;
|
|
20
|
+
pageMetadata: PageMetadata;
|
|
21
|
+
}
|
|
22
|
+
export interface PageMetadata {
|
|
23
|
+
path: string;
|
|
24
|
+
title?: string | null;
|
|
25
|
+
description?: string | null;
|
|
26
|
+
openGraphImageUrl?: string | null;
|
|
27
|
+
}
|
|
28
|
+
export interface GlobalGroupMeta {
|
|
29
|
+
id: string;
|
|
30
|
+
projectId: string;
|
|
31
|
+
name: string;
|
|
32
|
+
type: string;
|
|
33
|
+
contextFile: string;
|
|
34
|
+
useName: string;
|
|
35
|
+
}
|
|
36
|
+
export interface ProjectMeta {
|
|
37
|
+
id: string;
|
|
38
|
+
name: string;
|
|
39
|
+
version: string;
|
|
40
|
+
remoteFonts: FontMeta[];
|
|
41
|
+
globalContextsProviderFileName: string;
|
|
42
|
+
}
|
|
43
|
+
export interface FontMeta {
|
|
44
|
+
url: string;
|
|
45
|
+
}
|
|
46
|
+
interface GlobalVariantSplitContent {
|
|
47
|
+
type: 'global-variant';
|
|
48
|
+
projectId: string;
|
|
49
|
+
group: string;
|
|
50
|
+
variant: string;
|
|
51
|
+
}
|
|
52
|
+
interface Slice {
|
|
53
|
+
id: string;
|
|
54
|
+
contents: GlobalVariantSplitContent[];
|
|
55
|
+
externalId?: string;
|
|
56
|
+
}
|
|
57
|
+
export interface ExperimentSlice extends Slice {
|
|
58
|
+
prob: number;
|
|
59
|
+
}
|
|
60
|
+
export interface SegmentSlice extends Slice {
|
|
61
|
+
cond: any;
|
|
62
|
+
}
|
|
63
|
+
export interface ExperimentSplit {
|
|
64
|
+
id: string;
|
|
65
|
+
externalId?: string;
|
|
66
|
+
type: 'experiment';
|
|
67
|
+
slices: ExperimentSlice[];
|
|
68
|
+
}
|
|
69
|
+
export interface SegmentSplit {
|
|
70
|
+
id: string;
|
|
71
|
+
externalId?: string;
|
|
72
|
+
type: 'segment';
|
|
73
|
+
slices: SegmentSlice[];
|
|
74
|
+
}
|
|
75
|
+
export declare type Split = ExperimentSplit | SegmentSplit;
|
|
76
|
+
export interface LoaderBundleOutput {
|
|
77
|
+
modules: {
|
|
78
|
+
browser: (CodeModule | AssetModule)[];
|
|
79
|
+
server: (CodeModule | AssetModule)[];
|
|
80
|
+
};
|
|
81
|
+
external: string[];
|
|
82
|
+
components: ComponentMeta[];
|
|
83
|
+
globalGroups: GlobalGroupMeta[];
|
|
84
|
+
projects: ProjectMeta[];
|
|
85
|
+
activeSplits: Split[];
|
|
86
|
+
}
|
|
87
|
+
export interface LoaderHtmlOutput {
|
|
88
|
+
html: string;
|
|
89
|
+
}
|
|
90
|
+
export interface CodeModule {
|
|
91
|
+
fileName: string;
|
|
92
|
+
code: string;
|
|
93
|
+
imports: string[];
|
|
94
|
+
type: 'code';
|
|
95
|
+
}
|
|
96
|
+
export interface AssetModule {
|
|
97
|
+
fileName: string;
|
|
98
|
+
source: string;
|
|
99
|
+
type: 'asset';
|
|
100
|
+
}
|
|
101
|
+
export declare const isBrowser: boolean;
|
|
102
|
+
export declare class Api {
|
|
103
|
+
private opts;
|
|
104
|
+
private host;
|
|
105
|
+
constructor(opts: {
|
|
106
|
+
projects: {
|
|
107
|
+
id: string;
|
|
108
|
+
token: string;
|
|
109
|
+
}[];
|
|
110
|
+
host?: string;
|
|
111
|
+
});
|
|
112
|
+
fetchLoaderData(projectIds: string[], opts: {
|
|
113
|
+
platform?: 'react' | 'nextjs' | 'gatsby';
|
|
114
|
+
preview?: boolean;
|
|
115
|
+
browserOnly?: boolean;
|
|
116
|
+
}): Promise<LoaderBundleOutput>;
|
|
117
|
+
fetchHtmlData(opts: {
|
|
118
|
+
projectId: string;
|
|
119
|
+
component: string;
|
|
120
|
+
hydrate?: boolean;
|
|
121
|
+
embedHydrate?: boolean;
|
|
122
|
+
}): Promise<LoaderHtmlOutput>;
|
|
123
|
+
private makeGetHeaders;
|
|
124
|
+
private makePostHeaders;
|
|
125
|
+
private makeAuthHeaders;
|
|
126
|
+
}
|
|
127
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { LoaderBundleOutput } from './api';
|
|
2
|
+
export interface FetcherOptions {
|
|
3
|
+
projects: {
|
|
4
|
+
id: string;
|
|
5
|
+
version?: string;
|
|
6
|
+
token: string;
|
|
7
|
+
}[];
|
|
8
|
+
cache?: LoaderBundleCache;
|
|
9
|
+
platform?: 'react' | 'nextjs' | 'gatsby';
|
|
10
|
+
preview?: boolean;
|
|
11
|
+
host?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface LoaderBundleCache {
|
|
14
|
+
set: (data: LoaderBundleOutput) => Promise<void>;
|
|
15
|
+
get: () => Promise<LoaderBundleOutput>;
|
|
16
|
+
}
|
|
17
|
+
export declare class PlasmicModulesFetcher {
|
|
18
|
+
private opts;
|
|
19
|
+
private api;
|
|
20
|
+
private curFetch;
|
|
21
|
+
constructor(opts: FetcherOptions);
|
|
22
|
+
fetchAllData(): Promise<LoaderBundleOutput>;
|
|
23
|
+
private doFetch;
|
|
24
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { Api, AssetModule, CodeModule, ComponentMeta, ExperimentSlice, FontMeta, GlobalGroupMeta, LoaderBundleOutput, LoaderHtmlOutput, PageMeta, PageMetadata, ProjectMeta, SegmentSlice, Split, } from './api';
|
|
2
|
+
export { FetcherOptions, LoaderBundleCache, PlasmicModulesFetcher, } from './fetcher';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
6
|
+
|
|
7
|
+
var fetch = _interopDefault(require('isomorphic-unfetch'));
|
|
8
|
+
|
|
9
|
+
const VERSION = '3';
|
|
10
|
+
const isBrowser = typeof window !== 'undefined' && window != null && typeof window.document !== 'undefined';
|
|
11
|
+
class Api {
|
|
12
|
+
constructor(opts) {
|
|
13
|
+
var _opts$host;
|
|
14
|
+
|
|
15
|
+
this.opts = opts;
|
|
16
|
+
this.host = (_opts$host = opts.host) != null ? _opts$host : 'https://codegen.plasmic.app';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async fetchLoaderData(projectIds, opts) {
|
|
20
|
+
const {
|
|
21
|
+
platform,
|
|
22
|
+
preview
|
|
23
|
+
} = opts;
|
|
24
|
+
const query = new URLSearchParams([['platform', platform != null ? platform : 'react'], ...projectIds.map(projectId => ['projectId', projectId]), ...(opts.browserOnly ? [['browserOnly', 'true']] : [])]).toString();
|
|
25
|
+
const url = `${this.host}/api/v1/loader/code/${preview ? 'preview' : 'published'}?${query}`;
|
|
26
|
+
const resp = await fetch(url, {
|
|
27
|
+
method: 'GET',
|
|
28
|
+
headers: this.makeGetHeaders()
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (resp.status >= 400) {
|
|
32
|
+
var _error$error$message, _error$error;
|
|
33
|
+
|
|
34
|
+
const error = await resp.json();
|
|
35
|
+
throw new Error((_error$error$message = error == null ? void 0 : (_error$error = error.error) == null ? void 0 : _error$error.message) != null ? _error$error$message : resp.statusText);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const json = await resp.json();
|
|
39
|
+
return json;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async fetchHtmlData(opts) {
|
|
43
|
+
const {
|
|
44
|
+
projectId,
|
|
45
|
+
component,
|
|
46
|
+
embedHydrate,
|
|
47
|
+
hydrate
|
|
48
|
+
} = opts;
|
|
49
|
+
const query = new URLSearchParams([['projectId', projectId], ['component', component], ['embedHydrate', embedHydrate ? '1' : '0'], ['hydrate', hydrate ? '1' : '0']]).toString();
|
|
50
|
+
const resp = await fetch(`${this.host}/api/v1/loader/html?${query}`, {
|
|
51
|
+
method: 'GET',
|
|
52
|
+
headers: this.makeGetHeaders()
|
|
53
|
+
});
|
|
54
|
+
const json = await resp.json();
|
|
55
|
+
return json;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
makeGetHeaders() {
|
|
59
|
+
return {
|
|
60
|
+
'x-plasmic-loader-version': VERSION,
|
|
61
|
+
...this.makeAuthHeaders()
|
|
62
|
+
};
|
|
63
|
+
} // @ts-ignore
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
makePostHeaders() {
|
|
67
|
+
return {
|
|
68
|
+
'x-plasmic-loader-version': VERSION,
|
|
69
|
+
'Content-Type': 'application/json',
|
|
70
|
+
...this.makeAuthHeaders()
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
makeAuthHeaders() {
|
|
75
|
+
const tokens = this.opts.projects.map(p => `${p.id}:${p.token}`).join(',');
|
|
76
|
+
return {
|
|
77
|
+
'x-plasmic-api-project-tokens': tokens
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
class PlasmicModulesFetcher {
|
|
84
|
+
constructor(opts) {
|
|
85
|
+
this.opts = opts;
|
|
86
|
+
this.curFetch = undefined;
|
|
87
|
+
this.api = new Api({
|
|
88
|
+
projects: opts.projects,
|
|
89
|
+
host: opts.host
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async fetchAllData() {
|
|
94
|
+
if (this.opts.cache) {
|
|
95
|
+
const cachedData = await this.opts.cache.get();
|
|
96
|
+
|
|
97
|
+
if (cachedData) {
|
|
98
|
+
return cachedData;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (this.curFetch) {
|
|
103
|
+
return await this.curFetch;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
console.debug('Plasmic: doing a fresh fetch...');
|
|
107
|
+
this.curFetch = this.doFetch();
|
|
108
|
+
const data = await this.curFetch;
|
|
109
|
+
this.curFetch = undefined;
|
|
110
|
+
return data;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async doFetch() {
|
|
114
|
+
const data = await this.api.fetchLoaderData(this.opts.projects.map(p => p.version && !this.opts.preview ? `${p.id}@${p.version}` : p.id), {
|
|
115
|
+
platform: this.opts.platform,
|
|
116
|
+
preview: this.opts.preview,
|
|
117
|
+
browserOnly: isBrowser
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
if (this.opts.cache) {
|
|
121
|
+
await this.opts.cache.set(data);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
console.debug(`Plasmic: fetched designs for ${data.projects.map(p => `"${p.name}" (${p.id}@${p.version})`).join(', ')}`);
|
|
125
|
+
return data;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
exports.Api = Api;
|
|
131
|
+
exports.PlasmicModulesFetcher = PlasmicModulesFetcher;
|
|
132
|
+
//# sourceMappingURL=loader-fetcher.cjs.development.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader-fetcher.cjs.development.js","sources":["../src/api.ts","../src/fetcher.ts"],"sourcesContent":["import fetch from 'isomorphic-unfetch';\n\nexport interface ComponentMeta {\n id: string;\n usedComponents: string[];\n projectId: string;\n name: string;\n displayName: string;\n cssFile: string;\n path: string | undefined;\n isPage: boolean;\n plumeType?: string;\n entry: string;\n isCode: boolean;\n pageMetadata?: PageMetadata;\n metadata?: Record<string, string>;\n}\n\nexport interface PageMeta extends ComponentMeta {\n isPage: true;\n path: string;\n plumeType: never;\n pageMetadata: PageMetadata;\n}\n\nexport interface PageMetadata {\n path: string;\n title?: string | null;\n description?: string | null;\n openGraphImageUrl?: string | null;\n}\n\nexport interface GlobalGroupMeta {\n id: string;\n projectId: string;\n name: string;\n type: string;\n contextFile: string;\n useName: string;\n}\n\nexport interface ProjectMeta {\n id: string;\n name: string;\n version: string;\n remoteFonts: FontMeta[];\n globalContextsProviderFileName: string;\n}\n\nexport interface FontMeta {\n url: string;\n}\n\ninterface GlobalVariantSplitContent {\n type: 'global-variant';\n projectId: string;\n group: string;\n variant: string;\n}\n\ninterface Slice {\n id: string;\n contents: GlobalVariantSplitContent[];\n externalId?: string;\n}\n\nexport interface ExperimentSlice extends Slice {\n prob: number;\n}\n\nexport interface SegmentSlice extends Slice {\n cond: any;\n}\n\nexport interface ExperimentSplit {\n id: string;\n externalId?: string;\n type: 'experiment';\n slices: ExperimentSlice[];\n}\n\nexport interface SegmentSplit {\n id: string;\n externalId?: string;\n type: 'segment';\n slices: SegmentSlice[];\n}\n\nexport type Split = ExperimentSplit | SegmentSplit;\n\nexport interface LoaderBundleOutput {\n modules: {\n browser: (CodeModule | AssetModule)[];\n server: (CodeModule | AssetModule)[];\n };\n external: string[];\n components: ComponentMeta[];\n globalGroups: GlobalGroupMeta[];\n projects: ProjectMeta[];\n activeSplits: Split[];\n}\n\nexport interface LoaderHtmlOutput {\n html: string;\n}\n\nexport interface CodeModule {\n fileName: string;\n code: string;\n imports: string[];\n type: 'code';\n}\n\nexport interface AssetModule {\n fileName: string;\n source: string;\n type: 'asset';\n}\n\nconst VERSION = '3';\n\nexport const isBrowser =\n typeof window !== 'undefined' &&\n window != null &&\n typeof window.document !== 'undefined';\n\nexport class Api {\n private host: string;\n constructor(\n private opts: {\n projects: { id: string; token: string }[];\n host?: string;\n }\n ) {\n this.host = opts.host ?? 'https://codegen.plasmic.app';\n }\n\n async fetchLoaderData(\n projectIds: string[],\n opts: {\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n browserOnly?: boolean;\n }\n ) {\n const { platform, preview } = opts;\n const query = new URLSearchParams([\n ['platform', platform ?? 'react'],\n ...projectIds.map((projectId) => ['projectId', projectId]),\n ...(opts.browserOnly ? [['browserOnly', 'true']] : []),\n ]).toString();\n\n const url = `${this.host}/api/v1/loader/code/${\n preview ? 'preview' : 'published'\n }?${query}`;\n const resp = await fetch(url, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n if (resp.status >= 400) {\n const error = await resp.json();\n throw new Error(error?.error?.message ?? resp.statusText);\n }\n const json = await resp.json();\n return json as LoaderBundleOutput;\n }\n\n async fetchHtmlData(opts: {\n projectId: string;\n component: string;\n hydrate?: boolean;\n embedHydrate?: boolean;\n }) {\n const { projectId, component, embedHydrate, hydrate } = opts;\n const query = new URLSearchParams([\n ['projectId', projectId],\n ['component', component],\n ['embedHydrate', embedHydrate ? '1' : '0'],\n ['hydrate', hydrate ? '1' : '0'],\n ]).toString();\n const resp = await fetch(`${this.host}/api/v1/loader/html?${query}`, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n const json = await resp.json();\n return json as LoaderHtmlOutput;\n }\n\n private makeGetHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n ...this.makeAuthHeaders(),\n };\n }\n\n // @ts-ignore\n private makePostHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n 'Content-Type': 'application/json',\n ...this.makeAuthHeaders(),\n };\n }\n\n private makeAuthHeaders() {\n const tokens = this.opts.projects\n .map((p) => `${p.id}:${p.token}`)\n .join(',');\n return {\n 'x-plasmic-api-project-tokens': tokens,\n };\n }\n}\n","import { Api, isBrowser, LoaderBundleOutput } from './api';\n\nexport interface FetcherOptions {\n projects: {\n id: string;\n version?: string;\n token: string;\n }[];\n cache?: LoaderBundleCache;\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n host?: string;\n}\n\nexport interface LoaderBundleCache {\n set: (data: LoaderBundleOutput) => Promise<void>;\n get: () => Promise<LoaderBundleOutput>;\n}\n\nexport class PlasmicModulesFetcher {\n private api: Api;\n private curFetch: Promise<LoaderBundleOutput> | undefined = undefined;\n constructor(private opts: FetcherOptions) {\n this.api = new Api({\n projects: opts.projects,\n host: opts.host,\n });\n }\n\n async fetchAllData() {\n if (this.opts.cache) {\n const cachedData = await this.opts.cache.get();\n if (cachedData) {\n return cachedData;\n }\n }\n if (this.curFetch) {\n return await this.curFetch;\n }\n console.debug('Plasmic: doing a fresh fetch...');\n this.curFetch = this.doFetch();\n const data = await this.curFetch;\n this.curFetch = undefined;\n return data;\n }\n\n private async doFetch() {\n const data = await this.api.fetchLoaderData(\n this.opts.projects.map((p) =>\n p.version && !this.opts.preview ? `${p.id}@${p.version}` : p.id\n ),\n {\n platform: this.opts.platform,\n preview: this.opts.preview,\n browserOnly: isBrowser,\n }\n );\n if (this.opts.cache) {\n await this.opts.cache.set(data);\n }\n console.debug(\n `Plasmic: fetched designs for ${data.projects\n .map((p) => `\"${p.name}\" (${p.id}@${p.version})`)\n .join(', ')}`\n );\n return data;\n }\n}\n"],"names":["VERSION","isBrowser","window","document","Api","constructor","opts","host","fetchLoaderData","projectIds","platform","preview","query","URLSearchParams","map","projectId","browserOnly","toString","url","resp","fetch","method","headers","makeGetHeaders","status","error","json","Error","message","statusText","fetchHtmlData","component","embedHydrate","hydrate","makeAuthHeaders","makePostHeaders","tokens","projects","p","id","token","join","PlasmicModulesFetcher","undefined","api","fetchAllData","cache","cachedData","get","curFetch","console","debug","doFetch","data","version","set","name"],"mappings":";;;;;;;;AAuHA,MAAMA,OAAO,GAA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B,CAAN;AACD;;AACDH,IAAAA,OAAO,CAACC,KAAR,iCACkCE,IAAI,CAAChB,QAAL,CAC7BvB,GAD6B,CACxBwB,CAAD,QAAWA,CAAC,CAACkB,UAAUlB,CAAC,CAACC,MAAMD,CAAC,CAACgB,UADR,EAE7Bb,IAF6B,CAExB,IAFwB,GADlC;AAKA,WAAOY,IAAP;AACD;;;;;;;"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=(t=require("isomorphic-unfetch"))&&"object"==typeof t&&"default"in t?t.default:t;const s="undefined"!=typeof window&&null!=window&&void 0!==window.document;class o{constructor(t){var e;this.opts=t,this.host=null!=(e=t.host)?e:"https://codegen.plasmic.app"}async fetchLoaderData(t,s){const{platform:o,preview:r}=s,a=new URLSearchParams([["platform",null!=o?o:"react"],...t.map(t=>["projectId",t]),...s.browserOnly?[["browserOnly","true"]]:[]]).toString(),i=`${this.host}/api/v1/loader/code/${r?"preview":"published"}?${a}`,c=await e(i,{method:"GET",headers:this.makeGetHeaders()});if(c.status>=400){var n,h;const t=await c.json();throw new Error(null!=(n=null==t||null==(h=t.error)?void 0:h.message)?n:c.statusText)}return await c.json()}async fetchHtmlData(t){const{projectId:s,component:o,embedHydrate:r,hydrate:a}=t,i=new URLSearchParams([["projectId",s],["component",o],["embedHydrate",r?"1":"0"],["hydrate",a?"1":"0"]]).toString(),c=await e(`${this.host}/api/v1/loader/html?${i}`,{method:"GET",headers:this.makeGetHeaders()});return await c.json()}makeGetHeaders(){return{"x-plasmic-loader-version":"3",...this.makeAuthHeaders()}}makePostHeaders(){return{"x-plasmic-loader-version":"3","Content-Type":"application/json",...this.makeAuthHeaders()}}makeAuthHeaders(){return{"x-plasmic-api-project-tokens":this.opts.projects.map(t=>`${t.id}:${t.token}`).join(",")}}}exports.Api=o,exports.PlasmicModulesFetcher=class{constructor(t){this.opts=t,this.curFetch=void 0,this.api=new o({projects:t.projects,host:t.host})}async fetchAllData(){if(this.opts.cache){const t=await this.opts.cache.get();if(t)return t}if(this.curFetch)return await this.curFetch;console.debug("Plasmic: doing a fresh fetch..."),this.curFetch=this.doFetch();const t=await this.curFetch;return this.curFetch=void 0,t}async doFetch(){const t=await this.api.fetchLoaderData(this.opts.projects.map(t=>t.version&&!this.opts.preview?`${t.id}@${t.version}`:t.id),{platform:this.opts.platform,preview:this.opts.preview,browserOnly:s});return this.opts.cache&&await this.opts.cache.set(t),console.debug("Plasmic: fetched designs for "+t.projects.map(t=>`"${t.name}" (${t.id}@${t.version})`).join(", ")),t}};
|
|
2
|
+
//# sourceMappingURL=loader-fetcher.cjs.production.min.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader-fetcher.cjs.production.min.js","sources":["../src/api.ts","../src/fetcher.ts"],"sourcesContent":["import fetch from 'isomorphic-unfetch';\n\nexport interface ComponentMeta {\n id: string;\n usedComponents: string[];\n projectId: string;\n name: string;\n displayName: string;\n cssFile: string;\n path: string | undefined;\n isPage: boolean;\n plumeType?: string;\n entry: string;\n isCode: boolean;\n pageMetadata?: PageMetadata;\n metadata?: Record<string, string>;\n}\n\nexport interface PageMeta extends ComponentMeta {\n isPage: true;\n path: string;\n plumeType: never;\n pageMetadata: PageMetadata;\n}\n\nexport interface PageMetadata {\n path: string;\n title?: string | null;\n description?: string | null;\n openGraphImageUrl?: string | null;\n}\n\nexport interface GlobalGroupMeta {\n id: string;\n projectId: string;\n name: string;\n type: string;\n contextFile: string;\n useName: string;\n}\n\nexport interface ProjectMeta {\n id: string;\n name: string;\n version: string;\n remoteFonts: FontMeta[];\n globalContextsProviderFileName: string;\n}\n\nexport interface FontMeta {\n url: string;\n}\n\ninterface GlobalVariantSplitContent {\n type: 'global-variant';\n projectId: string;\n group: string;\n variant: string;\n}\n\ninterface Slice {\n id: string;\n contents: GlobalVariantSplitContent[];\n externalId?: string;\n}\n\nexport interface ExperimentSlice extends Slice {\n prob: number;\n}\n\nexport interface SegmentSlice extends Slice {\n cond: any;\n}\n\nexport interface ExperimentSplit {\n id: string;\n externalId?: string;\n type: 'experiment';\n slices: ExperimentSlice[];\n}\n\nexport interface SegmentSplit {\n id: string;\n externalId?: string;\n type: 'segment';\n slices: SegmentSlice[];\n}\n\nexport type Split = ExperimentSplit | SegmentSplit;\n\nexport interface LoaderBundleOutput {\n modules: {\n browser: (CodeModule | AssetModule)[];\n server: (CodeModule | AssetModule)[];\n };\n external: string[];\n components: ComponentMeta[];\n globalGroups: GlobalGroupMeta[];\n projects: ProjectMeta[];\n activeSplits: Split[];\n}\n\nexport interface LoaderHtmlOutput {\n html: string;\n}\n\nexport interface CodeModule {\n fileName: string;\n code: string;\n imports: string[];\n type: 'code';\n}\n\nexport interface AssetModule {\n fileName: string;\n source: string;\n type: 'asset';\n}\n\nconst VERSION = '3';\n\nexport const isBrowser =\n typeof window !== 'undefined' &&\n window != null &&\n typeof window.document !== 'undefined';\n\nexport class Api {\n private host: string;\n constructor(\n private opts: {\n projects: { id: string; token: string }[];\n host?: string;\n }\n ) {\n this.host = opts.host ?? 'https://codegen.plasmic.app';\n }\n\n async fetchLoaderData(\n projectIds: string[],\n opts: {\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n browserOnly?: boolean;\n }\n ) {\n const { platform, preview } = opts;\n const query = new URLSearchParams([\n ['platform', platform ?? 'react'],\n ...projectIds.map((projectId) => ['projectId', projectId]),\n ...(opts.browserOnly ? [['browserOnly', 'true']] : []),\n ]).toString();\n\n const url = `${this.host}/api/v1/loader/code/${\n preview ? 'preview' : 'published'\n }?${query}`;\n const resp = await fetch(url, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n if (resp.status >= 400) {\n const error = await resp.json();\n throw new Error(error?.error?.message ?? resp.statusText);\n }\n const json = await resp.json();\n return json as LoaderBundleOutput;\n }\n\n async fetchHtmlData(opts: {\n projectId: string;\n component: string;\n hydrate?: boolean;\n embedHydrate?: boolean;\n }) {\n const { projectId, component, embedHydrate, hydrate } = opts;\n const query = new URLSearchParams([\n ['projectId', projectId],\n ['component', component],\n ['embedHydrate', embedHydrate ? '1' : '0'],\n ['hydrate', hydrate ? '1' : '0'],\n ]).toString();\n const resp = await fetch(`${this.host}/api/v1/loader/html?${query}`, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n const json = await resp.json();\n return json as LoaderHtmlOutput;\n }\n\n private makeGetHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n ...this.makeAuthHeaders(),\n };\n }\n\n // @ts-ignore\n private makePostHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n 'Content-Type': 'application/json',\n ...this.makeAuthHeaders(),\n };\n }\n\n private makeAuthHeaders() {\n const tokens = this.opts.projects\n .map((p) => `${p.id}:${p.token}`)\n .join(',');\n return {\n 'x-plasmic-api-project-tokens': tokens,\n };\n }\n}\n","import { Api, isBrowser, LoaderBundleOutput } from './api';\n\nexport interface FetcherOptions {\n projects: {\n id: string;\n version?: string;\n token: string;\n }[];\n cache?: LoaderBundleCache;\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n host?: string;\n}\n\nexport interface LoaderBundleCache {\n set: (data: LoaderBundleOutput) => Promise<void>;\n get: () => Promise<LoaderBundleOutput>;\n}\n\nexport class PlasmicModulesFetcher {\n private api: Api;\n private curFetch: Promise<LoaderBundleOutput> | undefined = undefined;\n constructor(private opts: FetcherOptions) {\n this.api = new Api({\n projects: opts.projects,\n host: opts.host,\n });\n }\n\n async fetchAllData() {\n if (this.opts.cache) {\n const cachedData = await this.opts.cache.get();\n if (cachedData) {\n return cachedData;\n }\n }\n if (this.curFetch) {\n return await this.curFetch;\n }\n console.debug('Plasmic: doing a fresh fetch...');\n this.curFetch = this.doFetch();\n const data = await this.curFetch;\n this.curFetch = undefined;\n return data;\n }\n\n private async doFetch() {\n const data = await this.api.fetchLoaderData(\n this.opts.projects.map((p) =>\n p.version && !this.opts.preview ? `${p.id}@${p.version}` : p.id\n ),\n {\n platform: this.opts.platform,\n preview: this.opts.preview,\n browserOnly: isBrowser,\n }\n );\n if (this.opts.cache) {\n await this.opts.cache.set(data);\n }\n console.debug(\n `Plasmic: fetched designs for ${data.projects\n .map((p) => `\"${p.name}\" (${p.id}@${p.version})`)\n .join(', ')}`\n );\n return data;\n }\n}\n"],"names":["isBrowser","window","document","Api","constructor","opts","host","projectIds","platform","preview","query","URLSearchParams","map","projectId","browserOnly","toString","url","this","resp","fetch","method","headers","makeGetHeaders","status","error","json","Error","_error$error","message","statusText","component","embedHydrate","hydrate","makeAuthHeaders","makePostHeaders","projects","p","id","token","join","undefined","api","cache","cachedData","get","curFetch","console","debug","doFetch","data","fetchLoaderData","version","set","name"],"mappings":"6JAuHA,MAEaA,EACO,oBAAXC,QACG,MAAVA,aAC2B,IAApBA,OAAOC,eAEHC,EAEXC,YACUC,mBAAAA,OAKHC,cAAOD,EAAKC,QAAQ,oDAIzBC,EACAF,SAMMG,SAAEA,EAAFC,QAAYA,GAAYJ,EACxBK,EAAQ,IAAIC,gBAAgB,CAChC,CAAC,iBAAYH,EAAAA,EAAY,YACtBD,EAAWK,IAAKC,GAAc,CAAC,YAAaA,OAC3CR,EAAKS,YAAc,CAAC,CAAC,cAAe,SAAW,KAClDC,WAEGC,KAASC,KAAKX,2BAClBG,EAAU,UAAY,eACpBC,IACEQ,QAAaC,EAAMH,EAAK,CAC5BI,OAAQ,MACRC,QAASJ,KAAKK,sBAEZJ,EAAKK,QAAU,IAAK,eAChBC,QAAcN,EAAKO,aACnB,IAAIC,qBAAMF,YAAAA,EAAOA,cAAPG,EAAcC,WAAWV,EAAKW,yBAE7BX,EAAKO,2BAINpB,SAMZQ,UAAEA,EAAFiB,UAAaA,EAAbC,aAAwBA,EAAxBC,QAAsCA,GAAY3B,EAClDK,EAAQ,IAAIC,gBAAgB,CAChC,CAAC,YAAaE,GACd,CAAC,YAAaiB,GACd,CAAC,eAAgBC,EAAe,IAAM,KACtC,CAAC,UAAWC,EAAU,IAAM,OAC3BjB,WACGG,QAAaC,KAASF,KAAKX,2BAA2BI,IAAS,CACnEU,OAAQ,MACRC,QAASJ,KAAKK,gCAEGJ,EAAKO,OAIlBH,uBACC,4BAtEK,OAwEPL,KAAKgB,mBAKJC,wBACC,4BA9EK,mBAgFM,sBACbjB,KAAKgB,mBAIJA,wBAIC,gCAHQhB,KAAKZ,KAAK8B,SACtBvB,IAAKwB,MAASA,EAAEC,MAAMD,EAAEE,SACxBC,KAAK,yDCzLVnC,YAAoBC,aAAAA,qBADwCmC,OAErDC,IAAM,IAAItC,EAAI,CACjBgC,SAAU9B,EAAK8B,SACf7B,KAAMD,EAAKC,+BAKTW,KAAKZ,KAAKqC,MAAO,OACbC,QAAmB1B,KAAKZ,KAAKqC,MAAME,SACrCD,SACKA,KAGP1B,KAAK4B,sBACM5B,KAAK4B,SAEpBC,QAAQC,MAAM,wCACTF,SAAW5B,KAAK+B,gBACfC,QAAahC,KAAK4B,qBACnBA,cAAWL,EACTS,wBAIDA,QAAahC,KAAKwB,IAAIS,gBAC1BjC,KAAKZ,KAAK8B,SAASvB,IAAKwB,GACtBA,EAAEe,UAAYlC,KAAKZ,KAAKI,WAAa2B,EAAEC,MAAMD,EAAEe,UAAYf,EAAEC,IAE/D,CACE7B,SAAUS,KAAKZ,KAAKG,SACpBC,QAASQ,KAAKZ,KAAKI,QACnBK,YAAad,WAGbiB,KAAKZ,KAAKqC,aACNzB,KAAKZ,KAAKqC,MAAMU,IAAIH,GAE5BH,QAAQC,sCAC0BE,EAAKd,SAClCvB,IAAKwB,OAAUA,EAAEiB,UAAUjB,EAAEC,MAAMD,EAAEe,YACrCZ,KAAK,OAEHU"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import fetch from 'isomorphic-unfetch';
|
|
2
|
+
|
|
3
|
+
const VERSION = '3';
|
|
4
|
+
const isBrowser = typeof window !== 'undefined' && window != null && typeof window.document !== 'undefined';
|
|
5
|
+
class Api {
|
|
6
|
+
constructor(opts) {
|
|
7
|
+
var _opts$host;
|
|
8
|
+
|
|
9
|
+
this.opts = opts;
|
|
10
|
+
this.host = (_opts$host = opts.host) != null ? _opts$host : 'https://codegen.plasmic.app';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async fetchLoaderData(projectIds, opts) {
|
|
14
|
+
const {
|
|
15
|
+
platform,
|
|
16
|
+
preview
|
|
17
|
+
} = opts;
|
|
18
|
+
const query = new URLSearchParams([['platform', platform != null ? platform : 'react'], ...projectIds.map(projectId => ['projectId', projectId]), ...(opts.browserOnly ? [['browserOnly', 'true']] : [])]).toString();
|
|
19
|
+
const url = `${this.host}/api/v1/loader/code/${preview ? 'preview' : 'published'}?${query}`;
|
|
20
|
+
const resp = await fetch(url, {
|
|
21
|
+
method: 'GET',
|
|
22
|
+
headers: this.makeGetHeaders()
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
if (resp.status >= 400) {
|
|
26
|
+
var _error$error$message, _error$error;
|
|
27
|
+
|
|
28
|
+
const error = await resp.json();
|
|
29
|
+
throw new Error((_error$error$message = error == null ? void 0 : (_error$error = error.error) == null ? void 0 : _error$error.message) != null ? _error$error$message : resp.statusText);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const json = await resp.json();
|
|
33
|
+
return json;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async fetchHtmlData(opts) {
|
|
37
|
+
const {
|
|
38
|
+
projectId,
|
|
39
|
+
component,
|
|
40
|
+
embedHydrate,
|
|
41
|
+
hydrate
|
|
42
|
+
} = opts;
|
|
43
|
+
const query = new URLSearchParams([['projectId', projectId], ['component', component], ['embedHydrate', embedHydrate ? '1' : '0'], ['hydrate', hydrate ? '1' : '0']]).toString();
|
|
44
|
+
const resp = await fetch(`${this.host}/api/v1/loader/html?${query}`, {
|
|
45
|
+
method: 'GET',
|
|
46
|
+
headers: this.makeGetHeaders()
|
|
47
|
+
});
|
|
48
|
+
const json = await resp.json();
|
|
49
|
+
return json;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
makeGetHeaders() {
|
|
53
|
+
return {
|
|
54
|
+
'x-plasmic-loader-version': VERSION,
|
|
55
|
+
...this.makeAuthHeaders()
|
|
56
|
+
};
|
|
57
|
+
} // @ts-ignore
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
makePostHeaders() {
|
|
61
|
+
return {
|
|
62
|
+
'x-plasmic-loader-version': VERSION,
|
|
63
|
+
'Content-Type': 'application/json',
|
|
64
|
+
...this.makeAuthHeaders()
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
makeAuthHeaders() {
|
|
69
|
+
const tokens = this.opts.projects.map(p => `${p.id}:${p.token}`).join(',');
|
|
70
|
+
return {
|
|
71
|
+
'x-plasmic-api-project-tokens': tokens
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
class PlasmicModulesFetcher {
|
|
78
|
+
constructor(opts) {
|
|
79
|
+
this.opts = opts;
|
|
80
|
+
this.curFetch = undefined;
|
|
81
|
+
this.api = new Api({
|
|
82
|
+
projects: opts.projects,
|
|
83
|
+
host: opts.host
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async fetchAllData() {
|
|
88
|
+
if (this.opts.cache) {
|
|
89
|
+
const cachedData = await this.opts.cache.get();
|
|
90
|
+
|
|
91
|
+
if (cachedData) {
|
|
92
|
+
return cachedData;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (this.curFetch) {
|
|
97
|
+
return await this.curFetch;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
console.debug('Plasmic: doing a fresh fetch...');
|
|
101
|
+
this.curFetch = this.doFetch();
|
|
102
|
+
const data = await this.curFetch;
|
|
103
|
+
this.curFetch = undefined;
|
|
104
|
+
return data;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async doFetch() {
|
|
108
|
+
const data = await this.api.fetchLoaderData(this.opts.projects.map(p => p.version && !this.opts.preview ? `${p.id}@${p.version}` : p.id), {
|
|
109
|
+
platform: this.opts.platform,
|
|
110
|
+
preview: this.opts.preview,
|
|
111
|
+
browserOnly: isBrowser
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
if (this.opts.cache) {
|
|
115
|
+
await this.opts.cache.set(data);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
console.debug(`Plasmic: fetched designs for ${data.projects.map(p => `"${p.name}" (${p.id}@${p.version})`).join(', ')}`);
|
|
119
|
+
return data;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export { Api, PlasmicModulesFetcher };
|
|
125
|
+
//# sourceMappingURL=loader-fetcher.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader-fetcher.esm.js","sources":["../src/api.ts","../src/fetcher.ts"],"sourcesContent":["import fetch from 'isomorphic-unfetch';\n\nexport interface ComponentMeta {\n id: string;\n usedComponents: string[];\n projectId: string;\n name: string;\n displayName: string;\n cssFile: string;\n path: string | undefined;\n isPage: boolean;\n plumeType?: string;\n entry: string;\n isCode: boolean;\n pageMetadata?: PageMetadata;\n metadata?: Record<string, string>;\n}\n\nexport interface PageMeta extends ComponentMeta {\n isPage: true;\n path: string;\n plumeType: never;\n pageMetadata: PageMetadata;\n}\n\nexport interface PageMetadata {\n path: string;\n title?: string | null;\n description?: string | null;\n openGraphImageUrl?: string | null;\n}\n\nexport interface GlobalGroupMeta {\n id: string;\n projectId: string;\n name: string;\n type: string;\n contextFile: string;\n useName: string;\n}\n\nexport interface ProjectMeta {\n id: string;\n name: string;\n version: string;\n remoteFonts: FontMeta[];\n globalContextsProviderFileName: string;\n}\n\nexport interface FontMeta {\n url: string;\n}\n\ninterface GlobalVariantSplitContent {\n type: 'global-variant';\n projectId: string;\n group: string;\n variant: string;\n}\n\ninterface Slice {\n id: string;\n contents: GlobalVariantSplitContent[];\n externalId?: string;\n}\n\nexport interface ExperimentSlice extends Slice {\n prob: number;\n}\n\nexport interface SegmentSlice extends Slice {\n cond: any;\n}\n\nexport interface ExperimentSplit {\n id: string;\n externalId?: string;\n type: 'experiment';\n slices: ExperimentSlice[];\n}\n\nexport interface SegmentSplit {\n id: string;\n externalId?: string;\n type: 'segment';\n slices: SegmentSlice[];\n}\n\nexport type Split = ExperimentSplit | SegmentSplit;\n\nexport interface LoaderBundleOutput {\n modules: {\n browser: (CodeModule | AssetModule)[];\n server: (CodeModule | AssetModule)[];\n };\n external: string[];\n components: ComponentMeta[];\n globalGroups: GlobalGroupMeta[];\n projects: ProjectMeta[];\n activeSplits: Split[];\n}\n\nexport interface LoaderHtmlOutput {\n html: string;\n}\n\nexport interface CodeModule {\n fileName: string;\n code: string;\n imports: string[];\n type: 'code';\n}\n\nexport interface AssetModule {\n fileName: string;\n source: string;\n type: 'asset';\n}\n\nconst VERSION = '3';\n\nexport const isBrowser =\n typeof window !== 'undefined' &&\n window != null &&\n typeof window.document !== 'undefined';\n\nexport class Api {\n private host: string;\n constructor(\n private opts: {\n projects: { id: string; token: string }[];\n host?: string;\n }\n ) {\n this.host = opts.host ?? 'https://codegen.plasmic.app';\n }\n\n async fetchLoaderData(\n projectIds: string[],\n opts: {\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n browserOnly?: boolean;\n }\n ) {\n const { platform, preview } = opts;\n const query = new URLSearchParams([\n ['platform', platform ?? 'react'],\n ...projectIds.map((projectId) => ['projectId', projectId]),\n ...(opts.browserOnly ? [['browserOnly', 'true']] : []),\n ]).toString();\n\n const url = `${this.host}/api/v1/loader/code/${\n preview ? 'preview' : 'published'\n }?${query}`;\n const resp = await fetch(url, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n if (resp.status >= 400) {\n const error = await resp.json();\n throw new Error(error?.error?.message ?? resp.statusText);\n }\n const json = await resp.json();\n return json as LoaderBundleOutput;\n }\n\n async fetchHtmlData(opts: {\n projectId: string;\n component: string;\n hydrate?: boolean;\n embedHydrate?: boolean;\n }) {\n const { projectId, component, embedHydrate, hydrate } = opts;\n const query = new URLSearchParams([\n ['projectId', projectId],\n ['component', component],\n ['embedHydrate', embedHydrate ? '1' : '0'],\n ['hydrate', hydrate ? '1' : '0'],\n ]).toString();\n const resp = await fetch(`${this.host}/api/v1/loader/html?${query}`, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n const json = await resp.json();\n return json as LoaderHtmlOutput;\n }\n\n private makeGetHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n ...this.makeAuthHeaders(),\n };\n }\n\n // @ts-ignore\n private makePostHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n 'Content-Type': 'application/json',\n ...this.makeAuthHeaders(),\n };\n }\n\n private makeAuthHeaders() {\n const tokens = this.opts.projects\n .map((p) => `${p.id}:${p.token}`)\n .join(',');\n return {\n 'x-plasmic-api-project-tokens': tokens,\n };\n }\n}\n","import { Api, isBrowser, LoaderBundleOutput } from './api';\n\nexport interface FetcherOptions {\n projects: {\n id: string;\n version?: string;\n token: string;\n }[];\n cache?: LoaderBundleCache;\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n host?: string;\n}\n\nexport interface LoaderBundleCache {\n set: (data: LoaderBundleOutput) => Promise<void>;\n get: () => Promise<LoaderBundleOutput>;\n}\n\nexport class PlasmicModulesFetcher {\n private api: Api;\n private curFetch: Promise<LoaderBundleOutput> | undefined = undefined;\n constructor(private opts: FetcherOptions) {\n this.api = new Api({\n projects: opts.projects,\n host: opts.host,\n });\n }\n\n async fetchAllData() {\n if (this.opts.cache) {\n const cachedData = await this.opts.cache.get();\n if (cachedData) {\n return cachedData;\n }\n }\n if (this.curFetch) {\n return await this.curFetch;\n }\n console.debug('Plasmic: doing a fresh fetch...');\n this.curFetch = this.doFetch();\n const data = await this.curFetch;\n this.curFetch = undefined;\n return data;\n }\n\n private async doFetch() {\n const data = await this.api.fetchLoaderData(\n this.opts.projects.map((p) =>\n p.version && !this.opts.preview ? `${p.id}@${p.version}` : p.id\n ),\n {\n platform: this.opts.platform,\n preview: this.opts.preview,\n browserOnly: isBrowser,\n }\n );\n if (this.opts.cache) {\n await this.opts.cache.set(data);\n }\n console.debug(\n `Plasmic: fetched designs for ${data.projects\n .map((p) => `\"${p.name}\" (${p.id}@${p.version})`)\n .join(', ')}`\n );\n return data;\n }\n}\n"],"names":["VERSION","isBrowser","window","document","Api","constructor","opts","host","fetchLoaderData","projectIds","platform","preview","query","URLSearchParams","map","projectId","browserOnly","toString","url","resp","fetch","method","headers","makeGetHeaders","status","error","json","Error","message","statusText","fetchHtmlData","component","embedHydrate","hydrate","makeAuthHeaders","makePostHeaders","tokens","projects","p","id","token","join","PlasmicModulesFetcher","undefined","api","fetchAllData","cache","cachedData","get","curFetch","console","debug","doFetch","data","version","set","name"],"mappings":";;AAuHA,MAAMA,OAAO,GAAG,GAAhB;AAEO,MAAMC,SAAS,GACpB,OAAOC,MAAP,KAAkB,WAAlB,IACAA,MAAM,IAAI,IADV,IAEA,OAAOA,MAAM,CAACC,QAAd,KAA2B,WAHtB;MAKMC;AAEXC,EAAAA,YACUC;;;AAAA,aAAA,GAAAA,IAAA;AAKR,SAAKC,IAAL,iBAAYD,IAAI,CAACC,IAAjB,yBAAyB,6BAAzB;AACD;;AAEoB,QAAfC,eAAe,CACnBC,UADmB,EAEnBH,IAFmB;AAQnB,UAAM;AAAEI,MAAAA,QAAF;AAAYC,MAAAA;AAAZ,QAAwBL,IAA9B;AACA,UAAMM,KAAK,GAAG,IAAIC,eAAJ,CAAoB,CAChC,CAAC,UAAD,EAAaH,QAAb,WAAaA,QAAb,GAAyB,OAAzB,CADgC,EAEhC,GAAGD,UAAU,CAACK,GAAX,CAAgBC,SAAD,IAAe,CAAC,WAAD,EAAcA,SAAd,CAA9B,CAF6B,EAGhC,IAAIT,IAAI,CAACU,WAAL,GAAmB,CAAC,CAAC,aAAD,EAAgB,MAAhB,CAAD,CAAnB,GAA+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AAO;AACnB,UAAMC,IAAI,GAAG,MAAM,KAAKT,GAAL,CAASpC,eAAT,CACjB,KAAKF,IAAL,CAAU+B,QAAV,CAAmBvB,GAAnB,CAAwBwB,CAAD,IACrBA,CAAC,CAACgB,OAAF,IAAa,CAAC,KAAKhD,IAAL,CAAUK,OAAxB,MAAqC2B,CAAC,CAACC,MAAMD,CAAC,CAACgB,SAA/C,GAA2DhB,CAAC,CAACC,EAD/D,CADiB,EAIjB;AACE7B,MAAAA,QAAQ,EAAE,KAAKJ,IAAL,CAAUI,QADtB;AAEEC,MAAAA,OAAO,EAAE,KAAKL,IAAL,CAAUK,OAFrB;AAGEK,MAAAA,WAAW,EAAEf;AAHf,KAJiB,CAAnB;;AAUA,QAAI,KAAKK,IAAL,CAAUwC,KAAd,EAAqB;AACnB,YAAM,KAAKxC,IAAL,CAAUwC,KAAV,CAAgBS,GAAhB,CAAoBF,IAApB,CAAN;AACD;;AACDH,IAAAA,OAAO,CAACC,KAAR,iCACkCE,IAAI,CAAChB,QAAL,CAC7BvB,GAD6B,CACxBwB,CAAD,QAAWA,CAAC,CAACkB,UAAUlB,CAAC,CAACC,MAAMD,CAAC,CAACgB,UADR,EAE7Bb,IAF6B,CAExB,IAFwB,GADlC;AAKA,WAAOY,IAAP;AACD;;;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "1.0.1",
|
|
3
|
+
"license": "MIT",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"typings": "dist/index.d.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=10"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"start": "tsdx watch",
|
|
14
|
+
"build": "tsdx build --target node",
|
|
15
|
+
"yalcp": "yalc publish --push",
|
|
16
|
+
"test": "tsdx test",
|
|
17
|
+
"lint": "tsdx lint",
|
|
18
|
+
"prepare": "tsdx build --target node",
|
|
19
|
+
"size": "size-limit",
|
|
20
|
+
"analyze": "size-limit --why"
|
|
21
|
+
},
|
|
22
|
+
"husky": {
|
|
23
|
+
"hooks": {
|
|
24
|
+
"pre-commit": "tsdx lint"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"prettier": {
|
|
28
|
+
"printWidth": 80,
|
|
29
|
+
"semi": true,
|
|
30
|
+
"singleQuote": true,
|
|
31
|
+
"trailingComma": "es5"
|
|
32
|
+
},
|
|
33
|
+
"name": "@plasmicapp/loader-fetcher",
|
|
34
|
+
"author": "Chung Wu",
|
|
35
|
+
"module": "dist/loader-fetcher.esm.js",
|
|
36
|
+
"size-limit": [
|
|
37
|
+
{
|
|
38
|
+
"path": "dist/loader-fetcher.cjs.production.min.js",
|
|
39
|
+
"limit": "10 KB"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"path": "dist/loader-fetcher.esm.js",
|
|
43
|
+
"limit": "10 KB"
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@size-limit/preset-small-lib": "^4.11.0",
|
|
48
|
+
"@types/isomorphic-fetch": "^0.0.35",
|
|
49
|
+
"husky": "^6.0.0",
|
|
50
|
+
"size-limit": "^4.11.0",
|
|
51
|
+
"tsdx": "^0.14.1",
|
|
52
|
+
"tslib": "^2.2.0",
|
|
53
|
+
"typescript": "^4.3.2"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"isomorphic-unfetch": "^3.1.0"
|
|
57
|
+
},
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public"
|
|
60
|
+
},
|
|
61
|
+
"gitHead": "4ac59011cdf0689dabc2023db405627a232acf69"
|
|
62
|
+
}
|