@knapsack/app-client-api 4.109.0 → 4.110.2
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/app-client-api.d.ts +23 -1
- package/dist/app-client-api.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/app-client-api.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type KsAppClientData, type KsRenderResults, type Except, type GenericResponse, type KsAppClientDataAndMeta, type KsAppClientDataNoMeta, type KsFileSaverParams, type RendererId } from '@knapsack/types';
|
|
2
|
-
import type { PatternRenderData, EndpointPaths, EndpointFiles, ExtractEndpointData, ExtractEndpointResBody, EndpointPlugins, RouteDiscovery, RouteInspect, RouteRendererDiscovery } from '@knapsack/app/api-info';
|
|
2
|
+
import type { PatternRenderData, EndpointPaths, EndpointFiles, ExtractEndpointData, ExtractEndpointResBody, EndpointPlugins, RouteDiscovery, RouteInspect, RoutePkgInfo, RouteRendererDiscovery } from '@knapsack/app/api-info';
|
|
3
3
|
export type { PatternRenderData, EndpointPaths };
|
|
4
4
|
export declare function checkAppClientUrl(url: string): Promise<GenericResponse>;
|
|
5
5
|
export declare function getDataStore({ appClientUrl, }: {
|
|
@@ -60,4 +60,26 @@ export declare function inspectTemplate({ appClientUrl, templateInfo, authHeader
|
|
|
60
60
|
templateInfo: RouteInspect['input'];
|
|
61
61
|
authHeader: string;
|
|
62
62
|
}): Promise<RouteInspect['output']>;
|
|
63
|
+
/**
|
|
64
|
+
* `/api/v2/pkg-info` for one package: its `package.json` as the App Client
|
|
65
|
+
* resolves it from the site's own directory. A package the client cannot
|
|
66
|
+
* resolve is `pkg.notFound`; any other 500 from the client's own error
|
|
67
|
+
* handler (a `{ ok: false, message }` body) is `error.unknown` with that
|
|
68
|
+
* message, which can name paths on the client's machine. Throws on every
|
|
69
|
+
* other failure: a 401 from a private site, a 404 from a client without the
|
|
70
|
+
* route, a 500 from a proxy in front of the client, a network error.
|
|
71
|
+
*/
|
|
72
|
+
export declare function getPkgInfo({ appClientUrl, pkgName, authHeader, }: {
|
|
73
|
+
appClientUrl: string;
|
|
74
|
+
pkgName: string;
|
|
75
|
+
authHeader: string;
|
|
76
|
+
}): Promise<{
|
|
77
|
+
type: 'success';
|
|
78
|
+
data: RoutePkgInfo['output'];
|
|
79
|
+
} | {
|
|
80
|
+
type: 'pkg.notFound';
|
|
81
|
+
} | {
|
|
82
|
+
type: 'error.unknown';
|
|
83
|
+
message: string;
|
|
84
|
+
}>;
|
|
63
85
|
//# sourceMappingURL=app-client-api.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app-client-api.d.ts","sourceRoot":"","sources":["../src/app-client-api.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,KAAK,MAAM,EACX,KAAK,eAAe,EACpB,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC1B,KAAK,iBAAiB,EACtB,KAAK,UAAU,EAChB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,KAAK,EACV,iBAAiB,EAEjB,aAAa,EACb,aAAa,EACb,mBAAmB,EACnB,sBAAsB,EACtB,eAAe,EACf,cAAc,EACd,YAAY,EACZ,sBAAsB,EACvB,MAAM,wBAAwB,CAAC;AAEhC,YAAY,EAAE,iBAAiB,EAAE,aAAa,EAAE,CAAC;AAEjD,wBAAsB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,CAa7E;AAED,wBAAsB,YAAY,CAAC,EACjC,YAAY,GACb,EAAE;IACD,YAAY,EAAE,MAAM,CAAC;CACtB,GAAG,OAAO,CAAC,eAAe,CAAC,sBAAsB,CAAC,CAAC,CAgBnD;AAGD,wBAAsB,gBAAgB,CAAC,EACrC,YAAY,GACb,EAAE;IACD,YA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AC,OAAO,EAAE,MAAM,CAAA;CAAE,CAC7C,CA0BA"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';var utils=require('@knapsack/utils'),o=require('url-join');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var o__default=/*#__PURE__*/_interopDefault(o);var c=Object.defineProperty;var
|
|
1
|
+
'use strict';var utils=require('@knapsack/utils'),o=require('url-join');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var o__default=/*#__PURE__*/_interopDefault(o);var c=Object.defineProperty;var r=(n,e)=>c(n,"name",{value:e,configurable:true});async function E({appClientUrl:n,query:e}){return utils.fetcher({url:new URL("/api/v1/template-suggestions",n).toString(),query:e})}r(E,"getTemplateSuggestions");async function k(n){try{let e=o__default.default(n,"/api/v1");return await utils.fetcher({url:e})}catch(e){return {ok:false,message:e instanceof Error?e.message:"Unknown error"}}}r(k,"checkAppClientUrl");async function g({appClientUrl:n}){try{return {ok:true,data:await utils.fetcher({url:o__default.default(n,"/api/v1/data-store")})}}catch(e){return {ok:false,message:e instanceof Error?e.message:"Unknown error"}}}r(g,"getDataStore");async function x({appClientUrl:n}){let e=await g({appClientUrl:n});if(!e.ok||!e.data){let t=`We're having trouble accessing your server at ${n}.`;throw new Error(t)}return e.data}r(x,"getAppClientData");function d(n){let{metaState:e,...t}=n;return t}r(d,"prepAppClientData");async function C({urlBase:n,state:e}){try{let t={state:d(e)};return await utils.fetcher({url:o__default.default(n,"/api/v1/data-store-prep"),body:t})}catch(t){return t instanceof utils.FetcherError?{ok:false,message:t.message,meta:{status:t.status,statusText:t.statusText,url:t.url,responseHeaders:t.responseHeaders,responseBody:t.responseBody}}:{ok:false,message:t instanceof Error?t.message:"Unknown error"}}}r(C,"prepareDataForFileSave");async function w({urlBase:n,state:e}){let s={state:d(e)},i=await utils.fetcher({url:o__default.default(n,"/api/v1/data-store"),body:s});if(!i.ok)throw new Error(`Could not save data to localhost files: ${i.message}`)}r(w,"submitDataForFileSave");async function S({pluginId:n,appClientUrl:e}){return utils.fetcher({url:o__default.default(e,"/api/v1/plugins"),body:{pluginId:n,type:"getContent"}})}r(S,"getPluginContent");async function I({options:n,appClientUrl:e}){return await utils.fetcher({url:o__default.default(e,"/api/v1/render"),body:n})}r(I,"renderTemplate");async function A({appClientUrl:n,payload:e}){return (await utils.fetcher({url:o__default.default(n,"/api/v1/files"),body:{type:"verify",payload:e}})).payload}r(A,"verifyFile");async function K({appClientUrl:n,authHeader:e}){try{let t={};e&&(t.authorization=e);let s=await utils.fetcher({url:o__default.default(n,"/api/v2/discovery"),headers:t});if(!s)throw new Error("No result from discovery");return {type:"success",data:s}}catch(t){return t instanceof utils.FetcherError&&t.status===404?{type:"routeNotAvailable"}:{type:"routeNotAvailable"}}}r(K,"getDiscovery");async function F({appClientUrl:n,rendererId:e,authHeader:t}){let s={};return t&&(s.authorization=t),utils.fetcher({url:o__default.default(n,"/api/v2/renderer-discovery"),query:{rendererId:e},headers:s})}r(F,"getRendererDiscovery");async function T({appClientUrl:n,templateInfo:e,authHeader:t}){let s={};return t&&(s.authorization=t),utils.fetcher({url:o__default.default(n,"/api/v2/inspect"),query:e,headers:s})}r(T,"inspectTemplate");var f='Could not find package "';function y(n){if(n)try{let e=JSON.parse(n);if(typeof e=="object"&&e!==null&&"ok"in e&&e.ok===false&&"message"in e&&typeof e.message=="string")return e.message}catch{}}r(y,"getClientErrorMessage");async function U({appClientUrl:n,pkgName:e,authHeader:t}){let s={};t&&(s.authorization=t);try{return {type:"success",data:await utils.fetcher({url:o__default.default(n,"/api/v2/pkg-info"),query:{pkgName:e},headers:s})}}catch(i){let p=i instanceof utils.FetcherError&&i.status===500?y(i.responseBody):void 0;if(p===void 0)throw i;return p.startsWith(f)?{type:"pkg.notFound"}:{type:"error.unknown",message:p}}}r(U,"getPkgInfo");exports.checkAppClientUrl=k;exports.getAppClientData=x;exports.getDataStore=g;exports.getDiscovery=K;exports.getPkgInfo=U;exports.getPluginContent=S;exports.getRendererDiscovery=F;exports.getTemplateSuggestions=E;exports.inspectTemplate=T;exports.prepAppClientData=d;exports.prepareDataForFileSave=C;exports.renderTemplate=I;exports.submitDataForFileSave=w;exports.verifyFile=A;//# sourceMappingURL=index.js.map
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/get-template-suggestions.ts","../src/app-client-api.ts"],"names":["getTemplateSuggestions","appClientUrl","query","fetcher","__name","checkAppClientUrl","url","theUrl","urlJoin","err","getDataStore","getAppClientData","serverDataRes","message","prepAppClientData","state","metaState","appClientDataNoMeta","prepareDataForFileSave","urlBase","body","FetcherError","submitDataForFileSave","data","getPluginContent","pluginId","renderTemplate","options","verifyFile","payload","getDiscovery","authHeader","headers","error","getRendererDiscovery","rendererId","inspectTemplate","templateInfo"],"mappings":"wLAKA,IAAA,CAAA,CAAA,MAAA,CAAA,cAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,GAAA,CAAA,CAAA,CAAA,CAAA,MAAA,CAAA,CAAA,KAAA,CAAA,CAAA,CAAA,YAAA,CAAA,IAAA,CAAA,CAAA,CAEA,eAAsBA,CAAAA,CAAuB,CAC3C,YAAA,CAAAC,CAAAA,CACA,KAAA,CAAAC,CACF,CAAA,CAGwC,CACtC,OAAOC,aAAAA,CAAoC,CACzC,GAAA,CAAK,IAAI,GAAA,CACP,8BAAA,CACAF,CACF,CAAA,CAAE,QAAA,EAAS,CACX,KAAA,CAAAC,CACF,CAAC,CACH,CAdsBE,CAAAA,CAAAJ,EAAA,wBAAA,CAAA,CCoBtB,eAAsBK,CAAAA,CAAkBC,CAAAA,CAAuC,CAC7E,GAAI,CACF,IAAMC,CAAAA,CAASC,kBAAAA,CAAQF,CAAAA,CAAK,SAAiC,CAAA,CAI7D,OAHY,MAAMH,aAAAA,CAAyB,CACzC,GAAA,CAAKI,CACP,CAAC,CAEH,CAAA,MAASE,CAAAA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":["import type {\n EndpointPaths,\n TemplateSuggestionsReqParams,\n TemplateSuggestionsResBody,\n} from '@knapsack/app/api-info';\nimport { fetcher } from '@knapsack/utils';\n\nexport async function getTemplateSuggestions({\n appClientUrl,\n query,\n}: {\n appClientUrl: string;\n query: TemplateSuggestionsReqParams;\n}): Promise<TemplateSuggestionsResBody> {\n return fetcher<TemplateSuggestionsResBody>({\n url: new URL(\n '/api/v1/template-suggestions' satisfies EndpointPaths,\n appClientUrl,\n ).toString(),\n query,\n });\n}\n","import {\n type KsAppClientData,\n type KsRenderResults,\n type Except,\n type GenericResponse,\n type KsAppClientDataAndMeta,\n type KsAppClientDataNoMeta,\n type KsFileSaverParams,\n type RendererId,\n} from '@knapsack/types';\nimport { fetcher, FetcherError } from '@knapsack/utils';\nimport urlJoin from 'url-join';\nimport type {\n PatternRenderData,\n KnapsackDataStoreSaveBody,\n EndpointPaths,\n EndpointFiles,\n ExtractEndpointData,\n ExtractEndpointResBody,\n EndpointPlugins,\n RouteDiscovery,\n RouteInspect,\n RouteRendererDiscovery,\n} from '@knapsack/app/api-info';\n\nexport type { PatternRenderData, EndpointPaths };\n\nexport async function checkAppClientUrl(url: string): Promise<GenericResponse> {\n try {\n const theUrl = urlJoin(url, '/api/v1' satisfies EndpointPaths);\n const res = await fetcher<GenericResponse>({\n url: theUrl,\n });\n return res;\n } catch (err) {\n return {\n ok: false,\n message: err instanceof Error ? err.message : 'Unknown error',\n };\n }\n}\n\nexport async function getDataStore({\n appClientUrl,\n}: {\n appClientUrl: string;\n}): Promise<GenericResponse<KsAppClientDataAndMeta>> {\n try {\n const data = await fetcher<KsAppClientDataAndMeta>({\n url: urlJoin(appClientUrl, '/api/v1/data-store' satisfies EndpointPaths),\n });\n\n return {\n ok: true,\n data,\n };\n } catch (err) {\n return {\n ok: false,\n message: err instanceof Error ? err.message : 'Unknown error',\n };\n }\n}\n\n// just need a function that can throw an error\nexport async function getAppClientData({\n appClientUrl,\n}: {\n appClientUrl: string;\n}): Promise<KsAppClientDataAndMeta> {\n const serverDataRes = await getDataStore({ appClientUrl });\n if (!serverDataRes.ok || !serverDataRes.data) {\n const message = `We're having trouble accessing your server at ${appClientUrl}.`;\n throw new Error(message);\n }\n return serverDataRes.data;\n}\n\n/** Sole job is to strip `metaState` if it's there */\nexport function prepAppClientData(\n state: KsAppClientData,\n): KsAppClientDataNoMeta {\n const { metaState, ...appClientDataNoMeta } = state;\n return appClientDataNoMeta;\n}\n\nexport async function prepareDataForFileSave({\n urlBase,\n state,\n}: {\n urlBase: string;\n state: KsAppClientDataNoMeta;\n}): Promise<GenericResponse<KsFileSaverParams>> {\n try {\n const body: KnapsackDataStoreSaveBody = {\n state: prepAppClientData(state),\n };\n const data = await fetcher<GenericResponse<KsFileSaverParams>>({\n url: urlJoin(urlBase, '/api/v1/data-store-prep' satisfies EndpointPaths),\n body,\n });\n\n return data;\n } catch (err) {\n if (err instanceof FetcherError) {\n // Preserve the upstream response so the caller can log the actual\n // headers/body of the failure instead of just \"Error 500\".\n return {\n ok: false,\n message: err.message,\n meta: {\n status: err.status,\n statusText: err.statusText,\n url: err.url,\n responseHeaders: err.responseHeaders,\n responseBody: err.responseBody,\n },\n };\n }\n return {\n ok: false,\n message: err instanceof Error ? err.message : 'Unknown error',\n };\n }\n}\n\nexport async function submitDataForFileSave({\n urlBase,\n state,\n}: {\n urlBase: string;\n state: KsAppClientDataNoMeta;\n}): Promise<void> {\n const appClientData = prepAppClientData(state);\n const body: KnapsackDataStoreSaveBody = {\n state: appClientData,\n };\n const data = await fetcher<GenericResponse>({\n url: urlJoin(urlBase, '/api/v1/data-store' satisfies EndpointPaths),\n body,\n });\n\n if (!data.ok) {\n throw new Error(`Could not save data to localhost files: ${data.message}`);\n }\n}\n\nexport async function getPluginContent({\n pluginId,\n appClientUrl,\n}: {\n appClientUrl: string;\n} & Except<ExtractEndpointData<EndpointPlugins>, 'type'>): Promise<\n ExtractEndpointResBody<EndpointPlugins>\n> {\n return fetcher({\n url: urlJoin(\n appClientUrl,\n '/api/v1/plugins' satisfies EndpointPlugins['path'],\n ),\n body: {\n pluginId,\n type: 'getContent',\n } satisfies ExtractEndpointData<EndpointPlugins>,\n });\n}\n\nexport async function renderTemplate({\n options,\n appClientUrl,\n}: {\n options: PatternRenderData;\n appClientUrl: string;\n}): Promise<KsRenderResults> {\n const data = await fetcher<KsRenderResults>({\n url: urlJoin(appClientUrl, '/api/v1/render' satisfies EndpointPaths),\n body: options,\n });\n return data;\n}\n\nexport async function verifyFile({\n appClientUrl,\n payload,\n}: {\n appClientUrl: string;\n payload: ExtractEndpointData<EndpointFiles>['payload'];\n}) {\n const data = await fetcher<ExtractEndpointResBody<EndpointFiles>>({\n url: urlJoin(appClientUrl, '/api/v1/files' satisfies EndpointFiles['path']),\n body: {\n type: 'verify',\n payload,\n } satisfies ExtractEndpointData<EndpointFiles>,\n });\n return data.payload;\n}\n\nexport async function getDiscovery({\n appClientUrl,\n authHeader,\n}: {\n appClientUrl: string;\n authHeader: string;\n}): Promise<\n | {\n type: 'success';\n data: RouteDiscovery['output'];\n }\n | { type: 'routeNotAvailable' }\n> {\n try {\n const headers: Record<string, string> = {};\n if (authHeader) {\n headers.authorization = authHeader;\n }\n const data = await fetcher<RouteDiscovery['output']>({\n url: urlJoin(\n appClientUrl,\n '/api/v2/discovery' satisfies RouteDiscovery['path'],\n ),\n headers,\n });\n if (!data) {\n throw new Error('No result from discovery');\n }\n return {\n type: 'success',\n data,\n };\n } catch (error) {\n if (error instanceof FetcherError && error.status === 404) {\n return {\n type: 'routeNotAvailable',\n };\n }\n // being extra cautious here to avoid throwing an error\n return { type: 'routeNotAvailable' };\n }\n}\n\n/**\n * `/api/v2/renderer-discovery` for one renderer. Only for renderers the App\n * Client itself discovers; native mobile renderers are discovered by the App\n * API instead (`isRendererIdForNativeMobile`), so callers route those there.\n * Throws on any non-2xx answer, including the 401 a private site gives an\n * anonymous caller.\n */\nexport async function getRendererDiscovery<\n TheRendererId extends RendererId = RendererId,\n>({\n appClientUrl,\n rendererId,\n authHeader,\n}: {\n appClientUrl: string;\n rendererId: TheRendererId;\n authHeader: string;\n}): Promise<RouteRendererDiscovery<TheRendererId>['output']> {\n const headers: Record<string, string> = {};\n if (authHeader) {\n headers.authorization = authHeader;\n }\n return fetcher<RouteRendererDiscovery<TheRendererId>['output']>({\n url: urlJoin(\n appClientUrl,\n '/api/v2/renderer-discovery' satisfies RouteRendererDiscovery<TheRendererId>['path'],\n ),\n query: {\n rendererId,\n } satisfies RouteRendererDiscovery<TheRendererId>['input'],\n headers,\n });\n}\n\nexport async function inspectTemplate({\n appClientUrl,\n templateInfo,\n authHeader,\n}: {\n appClientUrl: string;\n templateInfo: RouteInspect['input'];\n authHeader: string;\n}): Promise<RouteInspect['output']> {\n const headers: Record<string, string> = {};\n if (authHeader) {\n headers.authorization = authHeader;\n }\n return fetcher({\n url: urlJoin(\n appClientUrl,\n '/api/v2/inspect' satisfies RouteInspect['path'],\n ),\n query: templateInfo,\n headers,\n });\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/get-template-suggestions.ts","../src/app-client-api.ts"],"names":["getTemplateSuggestions","appClientUrl","query","fetcher","__name","checkAppClientUrl","url","theUrl","urlJoin","err","getDataStore","getAppClientData","serverDataRes","message","prepAppClientData","state","metaState","appClientDataNoMeta","prepareDataForFileSave","urlBase","body","FetcherError","submitDataForFileSave","data","getPluginContent","pluginId","renderTemplate","options","verifyFile","payload","getDiscovery","authHeader","headers","error","getRendererDiscovery","rendererId","inspectTemplate","templateInfo","PKG_NOT_FOUND_MESSAGE_PREFIX","getClientErrorMessage","parsed","getPkgInfo","pkgName"],"mappings":"wLAKA,IAAA,CAAA,CAAA,MAAA,CAAA,cAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,GAAA,CAAA,CAAA,CAAA,CAAA,MAAA,CAAA,CAAA,KAAA,CAAA,CAAA,CAAA,YAAA,CAAA,IAAA,CAAA,CAAA,CAEA,eAAsBA,CAAAA,CAAuB,CAC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AAA,CAAAzB,CAAQ,CAC1C,CACF,CAtCsBT,EAAAqC,CAAAA,CAAA,YAAA,CAAA","file":"index.js","sourcesContent":["import type {\n EndpointPaths,\n TemplateSuggestionsReqParams,\n TemplateSuggestionsResBody,\n} from '@knapsack/app/api-info';\nimport { fetcher } from '@knapsack/utils';\n\nexport async function getTemplateSuggestions({\n appClientUrl,\n query,\n}: {\n appClientUrl: string;\n query: TemplateSuggestionsReqParams;\n}): Promise<TemplateSuggestionsResBody> {\n return fetcher<TemplateSuggestionsResBody>({\n url: new URL(\n '/api/v1/template-suggestions' satisfies EndpointPaths,\n appClientUrl,\n ).toString(),\n query,\n });\n}\n","import {\n type KsAppClientData,\n type KsRenderResults,\n type Except,\n type GenericResponse,\n type KsAppClientDataAndMeta,\n type KsAppClientDataNoMeta,\n type KsFileSaverParams,\n type RendererId,\n} from '@knapsack/types';\nimport { fetcher, FetcherError } from '@knapsack/utils';\nimport urlJoin from 'url-join';\nimport type {\n PatternRenderData,\n KnapsackDataStoreSaveBody,\n EndpointPaths,\n EndpointFiles,\n ExtractEndpointData,\n ExtractEndpointResBody,\n EndpointPlugins,\n RouteDiscovery,\n RouteInspect,\n RoutePkgInfo,\n RouteRendererDiscovery,\n} from '@knapsack/app/api-info';\n\nexport type { PatternRenderData, EndpointPaths };\n\nexport async function checkAppClientUrl(url: string): Promise<GenericResponse> {\n try {\n const theUrl = urlJoin(url, '/api/v1' satisfies EndpointPaths);\n const res = await fetcher<GenericResponse>({\n url: theUrl,\n });\n return res;\n } catch (err) {\n return {\n ok: false,\n message: err instanceof Error ? err.message : 'Unknown error',\n };\n }\n}\n\nexport async function getDataStore({\n appClientUrl,\n}: {\n appClientUrl: string;\n}): Promise<GenericResponse<KsAppClientDataAndMeta>> {\n try {\n const data = await fetcher<KsAppClientDataAndMeta>({\n url: urlJoin(appClientUrl, '/api/v1/data-store' satisfies EndpointPaths),\n });\n\n return {\n ok: true,\n data,\n };\n } catch (err) {\n return {\n ok: false,\n message: err instanceof Error ? err.message : 'Unknown error',\n };\n }\n}\n\n// just need a function that can throw an error\nexport async function getAppClientData({\n appClientUrl,\n}: {\n appClientUrl: string;\n}): Promise<KsAppClientDataAndMeta> {\n const serverDataRes = await getDataStore({ appClientUrl });\n if (!serverDataRes.ok || !serverDataRes.data) {\n const message = `We're having trouble accessing your server at ${appClientUrl}.`;\n throw new Error(message);\n }\n return serverDataRes.data;\n}\n\n/** Sole job is to strip `metaState` if it's there */\nexport function prepAppClientData(\n state: KsAppClientData,\n): KsAppClientDataNoMeta {\n const { metaState, ...appClientDataNoMeta } = state;\n return appClientDataNoMeta;\n}\n\nexport async function prepareDataForFileSave({\n urlBase,\n state,\n}: {\n urlBase: string;\n state: KsAppClientDataNoMeta;\n}): Promise<GenericResponse<KsFileSaverParams>> {\n try {\n const body: KnapsackDataStoreSaveBody = {\n state: prepAppClientData(state),\n };\n const data = await fetcher<GenericResponse<KsFileSaverParams>>({\n url: urlJoin(urlBase, '/api/v1/data-store-prep' satisfies EndpointPaths),\n body,\n });\n\n return data;\n } catch (err) {\n if (err instanceof FetcherError) {\n // Preserve the upstream response so the caller can log the actual\n // headers/body of the failure instead of just \"Error 500\".\n return {\n ok: false,\n message: err.message,\n meta: {\n status: err.status,\n statusText: err.statusText,\n url: err.url,\n responseHeaders: err.responseHeaders,\n responseBody: err.responseBody,\n },\n };\n }\n return {\n ok: false,\n message: err instanceof Error ? err.message : 'Unknown error',\n };\n }\n}\n\nexport async function submitDataForFileSave({\n urlBase,\n state,\n}: {\n urlBase: string;\n state: KsAppClientDataNoMeta;\n}): Promise<void> {\n const appClientData = prepAppClientData(state);\n const body: KnapsackDataStoreSaveBody = {\n state: appClientData,\n };\n const data = await fetcher<GenericResponse>({\n url: urlJoin(urlBase, '/api/v1/data-store' satisfies EndpointPaths),\n body,\n });\n\n if (!data.ok) {\n throw new Error(`Could not save data to localhost files: ${data.message}`);\n }\n}\n\nexport async function getPluginContent({\n pluginId,\n appClientUrl,\n}: {\n appClientUrl: string;\n} & Except<ExtractEndpointData<EndpointPlugins>, 'type'>): Promise<\n ExtractEndpointResBody<EndpointPlugins>\n> {\n return fetcher({\n url: urlJoin(\n appClientUrl,\n '/api/v1/plugins' satisfies EndpointPlugins['path'],\n ),\n body: {\n pluginId,\n type: 'getContent',\n } satisfies ExtractEndpointData<EndpointPlugins>,\n });\n}\n\nexport async function renderTemplate({\n options,\n appClientUrl,\n}: {\n options: PatternRenderData;\n appClientUrl: string;\n}): Promise<KsRenderResults> {\n const data = await fetcher<KsRenderResults>({\n url: urlJoin(appClientUrl, '/api/v1/render' satisfies EndpointPaths),\n body: options,\n });\n return data;\n}\n\nexport async function verifyFile({\n appClientUrl,\n payload,\n}: {\n appClientUrl: string;\n payload: ExtractEndpointData<EndpointFiles>['payload'];\n}) {\n const data = await fetcher<ExtractEndpointResBody<EndpointFiles>>({\n url: urlJoin(appClientUrl, '/api/v1/files' satisfies EndpointFiles['path']),\n body: {\n type: 'verify',\n payload,\n } satisfies ExtractEndpointData<EndpointFiles>,\n });\n return data.payload;\n}\n\nexport async function getDiscovery({\n appClientUrl,\n authHeader,\n}: {\n appClientUrl: string;\n authHeader: string;\n}): Promise<\n | {\n type: 'success';\n data: RouteDiscovery['output'];\n }\n | { type: 'routeNotAvailable' }\n> {\n try {\n const headers: Record<string, string> = {};\n if (authHeader) {\n headers.authorization = authHeader;\n }\n const data = await fetcher<RouteDiscovery['output']>({\n url: urlJoin(\n appClientUrl,\n '/api/v2/discovery' satisfies RouteDiscovery['path'],\n ),\n headers,\n });\n if (!data) {\n throw new Error('No result from discovery');\n }\n return {\n type: 'success',\n data,\n };\n } catch (error) {\n if (error instanceof FetcherError && error.status === 404) {\n return {\n type: 'routeNotAvailable',\n };\n }\n // being extra cautious here to avoid throwing an error\n return { type: 'routeNotAvailable' };\n }\n}\n\n/**\n * `/api/v2/renderer-discovery` for one renderer. Only for renderers the App\n * Client itself discovers; native mobile renderers are discovered by the App\n * API instead (`isRendererIdForNativeMobile`), so callers route those there.\n * Throws on any non-2xx answer, including the 401 a private site gives an\n * anonymous caller.\n */\nexport async function getRendererDiscovery<\n TheRendererId extends RendererId = RendererId,\n>({\n appClientUrl,\n rendererId,\n authHeader,\n}: {\n appClientUrl: string;\n rendererId: TheRendererId;\n authHeader: string;\n}): Promise<RouteRendererDiscovery<TheRendererId>['output']> {\n const headers: Record<string, string> = {};\n if (authHeader) {\n headers.authorization = authHeader;\n }\n return fetcher<RouteRendererDiscovery<TheRendererId>['output']>({\n url: urlJoin(\n appClientUrl,\n '/api/v2/renderer-discovery' satisfies RouteRendererDiscovery<TheRendererId>['path'],\n ),\n query: {\n rendererId,\n } satisfies RouteRendererDiscovery<TheRendererId>['input'],\n headers,\n });\n}\n\nexport async function inspectTemplate({\n appClientUrl,\n templateInfo,\n authHeader,\n}: {\n appClientUrl: string;\n templateInfo: RouteInspect['input'];\n authHeader: string;\n}): Promise<RouteInspect['output']> {\n const headers: Record<string, string> = {};\n if (authHeader) {\n headers.authorization = authHeader;\n }\n return fetcher({\n url: urlJoin(\n appClientUrl,\n '/api/v2/inspect' satisfies RouteInspect['path'],\n ),\n query: templateInfo,\n headers,\n });\n}\n\n/**\n * The App Client's error-handler message when `resolvePkg` cannot find the\n * package (`tools/file-utils/src/pkg-utils.ts`). The client sends no status\n * or type for it, only a 500 with this text, so the prefix is the signal.\n */\nconst PKG_NOT_FOUND_MESSAGE_PREFIX = 'Could not find package \"';\n\n/** The `message` of the App Client error handler's `{ ok: false, message }` body. */\nfunction getClientErrorMessage(body: string | undefined): string | undefined {\n if (!body) return undefined;\n try {\n const parsed: unknown = JSON.parse(body);\n if (\n typeof parsed === 'object' &&\n parsed !== null &&\n 'ok' in parsed &&\n parsed.ok === false &&\n 'message' in parsed &&\n typeof parsed.message === 'string'\n ) {\n return parsed.message;\n }\n } catch {\n // Not the client's JSON: a proxy's error page.\n }\n return undefined;\n}\n\n/**\n * `/api/v2/pkg-info` for one package: its `package.json` as the App Client\n * resolves it from the site's own directory. A package the client cannot\n * resolve is `pkg.notFound`; any other 500 from the client's own error\n * handler (a `{ ok: false, message }` body) is `error.unknown` with that\n * message, which can name paths on the client's machine. Throws on every\n * other failure: a 401 from a private site, a 404 from a client without the\n * route, a 500 from a proxy in front of the client, a network error.\n */\nexport async function getPkgInfo({\n appClientUrl,\n pkgName,\n authHeader,\n}: {\n appClientUrl: string;\n pkgName: string;\n authHeader: string;\n}): Promise<\n | { type: 'success'; data: RoutePkgInfo['output'] }\n | { type: 'pkg.notFound' }\n | { type: 'error.unknown'; message: string }\n> {\n const headers: Record<string, string> = {};\n if (authHeader) {\n headers.authorization = authHeader;\n }\n try {\n const data = await fetcher<RoutePkgInfo['output']>({\n url: urlJoin(\n appClientUrl,\n '/api/v2/pkg-info' satisfies RoutePkgInfo['path'],\n ),\n query: { pkgName } satisfies RoutePkgInfo['input'],\n headers,\n });\n return { type: 'success', data };\n } catch (error) {\n const message =\n error instanceof FetcherError && error.status === 500\n ? getClientErrorMessage(error.responseBody)\n : undefined;\n if (message === undefined) throw error;\n if (message.startsWith(PKG_NOT_FOUND_MESSAGE_PREFIX)) {\n return { type: 'pkg.notFound' };\n }\n return { type: 'error.unknown', message };\n }\n}\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {fetcher,FetcherError}from'@knapsack/utils';import o from'url-join';var c=Object.defineProperty;var
|
|
1
|
+
import {fetcher,FetcherError}from'@knapsack/utils';import o from'url-join';var c=Object.defineProperty;var r=(n,e)=>c(n,"name",{value:e,configurable:true});async function E({appClientUrl:n,query:e}){return fetcher({url:new URL("/api/v1/template-suggestions",n).toString(),query:e})}r(E,"getTemplateSuggestions");async function k(n){try{let e=o(n,"/api/v1");return await fetcher({url:e})}catch(e){return {ok:false,message:e instanceof Error?e.message:"Unknown error"}}}r(k,"checkAppClientUrl");async function g({appClientUrl:n}){try{return {ok:true,data:await fetcher({url:o(n,"/api/v1/data-store")})}}catch(e){return {ok:false,message:e instanceof Error?e.message:"Unknown error"}}}r(g,"getDataStore");async function x({appClientUrl:n}){let e=await g({appClientUrl:n});if(!e.ok||!e.data){let t=`We're having trouble accessing your server at ${n}.`;throw new Error(t)}return e.data}r(x,"getAppClientData");function d(n){let{metaState:e,...t}=n;return t}r(d,"prepAppClientData");async function C({urlBase:n,state:e}){try{let t={state:d(e)};return await fetcher({url:o(n,"/api/v1/data-store-prep"),body:t})}catch(t){return t instanceof FetcherError?{ok:false,message:t.message,meta:{status:t.status,statusText:t.statusText,url:t.url,responseHeaders:t.responseHeaders,responseBody:t.responseBody}}:{ok:false,message:t instanceof Error?t.message:"Unknown error"}}}r(C,"prepareDataForFileSave");async function w({urlBase:n,state:e}){let s={state:d(e)},i=await fetcher({url:o(n,"/api/v1/data-store"),body:s});if(!i.ok)throw new Error(`Could not save data to localhost files: ${i.message}`)}r(w,"submitDataForFileSave");async function S({pluginId:n,appClientUrl:e}){return fetcher({url:o(e,"/api/v1/plugins"),body:{pluginId:n,type:"getContent"}})}r(S,"getPluginContent");async function I({options:n,appClientUrl:e}){return await fetcher({url:o(e,"/api/v1/render"),body:n})}r(I,"renderTemplate");async function A({appClientUrl:n,payload:e}){return (await fetcher({url:o(n,"/api/v1/files"),body:{type:"verify",payload:e}})).payload}r(A,"verifyFile");async function K({appClientUrl:n,authHeader:e}){try{let t={};e&&(t.authorization=e);let s=await fetcher({url:o(n,"/api/v2/discovery"),headers:t});if(!s)throw new Error("No result from discovery");return {type:"success",data:s}}catch(t){return t instanceof FetcherError&&t.status===404?{type:"routeNotAvailable"}:{type:"routeNotAvailable"}}}r(K,"getDiscovery");async function F({appClientUrl:n,rendererId:e,authHeader:t}){let s={};return t&&(s.authorization=t),fetcher({url:o(n,"/api/v2/renderer-discovery"),query:{rendererId:e},headers:s})}r(F,"getRendererDiscovery");async function T({appClientUrl:n,templateInfo:e,authHeader:t}){let s={};return t&&(s.authorization=t),fetcher({url:o(n,"/api/v2/inspect"),query:e,headers:s})}r(T,"inspectTemplate");var f='Could not find package "';function y(n){if(n)try{let e=JSON.parse(n);if(typeof e=="object"&&e!==null&&"ok"in e&&e.ok===false&&"message"in e&&typeof e.message=="string")return e.message}catch{}}r(y,"getClientErrorMessage");async function U({appClientUrl:n,pkgName:e,authHeader:t}){let s={};t&&(s.authorization=t);try{return {type:"success",data:await fetcher({url:o(n,"/api/v2/pkg-info"),query:{pkgName:e},headers:s})}}catch(i){let p=i instanceof FetcherError&&i.status===500?y(i.responseBody):void 0;if(p===void 0)throw i;return p.startsWith(f)?{type:"pkg.notFound"}:{type:"error.unknown",message:p}}}r(U,"getPkgInfo");export{k as checkAppClientUrl,x as getAppClientData,g as getDataStore,K as getDiscovery,U as getPkgInfo,S as getPluginContent,F as getRendererDiscovery,E as getTemplateSuggestions,T as inspectTemplate,d as prepAppClientData,C as prepareDataForFileSave,I as renderTemplate,w as submitDataForFileSave,A as verifyFile};//# sourceMappingURL=index.mjs.map
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/get-template-suggestions.ts","../src/app-client-api.ts"],"names":["getTemplateSuggestions","appClientUrl","query","fetcher","__name","checkAppClientUrl","url","theUrl","urlJoin","err","getDataStore","getAppClientData","serverDataRes","message","prepAppClientData","state","metaState","appClientDataNoMeta","prepareDataForFileSave","urlBase","body","FetcherError","submitDataForFileSave","data","getPluginContent","pluginId","renderTemplate","options","verifyFile","payload","getDiscovery","authHeader","headers","error","getRendererDiscovery","rendererId","inspectTemplate","templateInfo"],"mappings":"2EAKA,IAAA,CAAA,CAAA,MAAA,CAAA,cAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,GAAA,CAAA,CAAA,CAAA,CAAA,MAAA,CAAA,CAAA,KAAA,CAAA,CAAA,CAAA,YAAA,CAAA,IAAA,CAAA,CAAA,CAEA,eAAsBA,CAAAA,CAAuB,CAC3C,YAAA,CAAAC,CAAAA,CACA,KAAA,CA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AAsBE,CAAAA,CAAgB,CACpC,YAAA,CAAAnC,CAAAA,CACA,YAAA,CAAAoC,CAAAA,CACA,UAAA,CAAAN,CACF,CAAA,CAIoC,CAClC,IAAMC,CAAAA,CAAkC,EAAC,CACzC,OAAID,CAAAA,GACFC,EAAQ,aAAA,CAAgBD,CAAAA,CAAAA,CAEnB5B,OAAAA,CAAQ,CACb,GAAA,CAAKK,CAAAA,CACHP,CAAAA,CACA,iBACF,CAAA,CACA,KAAA,CAAOoC,CAAAA,CACP,OAAA,CAAAL,CACF,CAAC,CACH,CArBsB5B,EAAAgC,CAAAA,CAAA,iBAAA,CAAA","file":"index.mjs","sourcesContent":["import type {\n EndpointPaths,\n TemplateSuggestionsReqParams,\n TemplateSuggestionsResBody,\n} from '@knapsack/app/api-info';\nimport { fetcher } from '@knapsack/utils';\n\nexport async function getTemplateSuggestions({\n appClientUrl,\n query,\n}: {\n appClientUrl: string;\n query: TemplateSuggestionsReqParams;\n}): Promise<TemplateSuggestionsResBody> {\n return fetcher<TemplateSuggestionsResBody>({\n url: new URL(\n '/api/v1/template-suggestions' satisfies EndpointPaths,\n appClientUrl,\n ).toString(),\n query,\n });\n}\n","import {\n type KsAppClientData,\n type KsRenderResults,\n type Except,\n type GenericResponse,\n type KsAppClientDataAndMeta,\n type KsAppClientDataNoMeta,\n type KsFileSaverParams,\n type RendererId,\n} from '@knapsack/types';\nimport { fetcher, FetcherError } from '@knapsack/utils';\nimport urlJoin from 'url-join';\nimport type {\n PatternRenderData,\n KnapsackDataStoreSaveBody,\n EndpointPaths,\n EndpointFiles,\n ExtractEndpointData,\n ExtractEndpointResBody,\n EndpointPlugins,\n RouteDiscovery,\n RouteInspect,\n RouteRendererDiscovery,\n} from '@knapsack/app/api-info';\n\nexport type { PatternRenderData, EndpointPaths };\n\nexport async function checkAppClientUrl(url: string): Promise<GenericResponse> {\n try {\n const theUrl = urlJoin(url, '/api/v1' satisfies EndpointPaths);\n const res = await fetcher<GenericResponse>({\n url: theUrl,\n });\n return res;\n } catch (err) {\n return {\n ok: false,\n message: err instanceof Error ? err.message : 'Unknown error',\n };\n }\n}\n\nexport async function getDataStore({\n appClientUrl,\n}: {\n appClientUrl: string;\n}): Promise<GenericResponse<KsAppClientDataAndMeta>> {\n try {\n const data = await fetcher<KsAppClientDataAndMeta>({\n url: urlJoin(appClientUrl, '/api/v1/data-store' satisfies EndpointPaths),\n });\n\n return {\n ok: true,\n data,\n };\n } catch (err) {\n return {\n ok: false,\n message: err instanceof Error ? err.message : 'Unknown error',\n };\n }\n}\n\n// just need a function that can throw an error\nexport async function getAppClientData({\n appClientUrl,\n}: {\n appClientUrl: string;\n}): Promise<KsAppClientDataAndMeta> {\n const serverDataRes = await getDataStore({ appClientUrl });\n if (!serverDataRes.ok || !serverDataRes.data) {\n const message = `We're having trouble accessing your server at ${appClientUrl}.`;\n throw new Error(message);\n }\n return serverDataRes.data;\n}\n\n/** Sole job is to strip `metaState` if it's there */\nexport function prepAppClientData(\n state: KsAppClientData,\n): KsAppClientDataNoMeta {\n const { metaState, ...appClientDataNoMeta } = state;\n return appClientDataNoMeta;\n}\n\nexport async function prepareDataForFileSave({\n urlBase,\n state,\n}: {\n urlBase: string;\n state: KsAppClientDataNoMeta;\n}): Promise<GenericResponse<KsFileSaverParams>> {\n try {\n const body: KnapsackDataStoreSaveBody = {\n state: prepAppClientData(state),\n };\n const data = await fetcher<GenericResponse<KsFileSaverParams>>({\n url: urlJoin(urlBase, '/api/v1/data-store-prep' satisfies EndpointPaths),\n body,\n });\n\n return data;\n } catch (err) {\n if (err instanceof FetcherError) {\n // Preserve the upstream response so the caller can log the actual\n // headers/body of the failure instead of just \"Error 500\".\n return {\n ok: false,\n message: err.message,\n meta: {\n status: err.status,\n statusText: err.statusText,\n url: err.url,\n responseHeaders: err.responseHeaders,\n responseBody: err.responseBody,\n },\n };\n }\n return {\n ok: false,\n message: err instanceof Error ? err.message : 'Unknown error',\n };\n }\n}\n\nexport async function submitDataForFileSave({\n urlBase,\n state,\n}: {\n urlBase: string;\n state: KsAppClientDataNoMeta;\n}): Promise<void> {\n const appClientData = prepAppClientData(state);\n const body: KnapsackDataStoreSaveBody = {\n state: appClientData,\n };\n const data = await fetcher<GenericResponse>({\n url: urlJoin(urlBase, '/api/v1/data-store' satisfies EndpointPaths),\n body,\n });\n\n if (!data.ok) {\n throw new Error(`Could not save data to localhost files: ${data.message}`);\n }\n}\n\nexport async function getPluginContent({\n pluginId,\n appClientUrl,\n}: {\n appClientUrl: string;\n} & Except<ExtractEndpointData<EndpointPlugins>, 'type'>): Promise<\n ExtractEndpointResBody<EndpointPlugins>\n> {\n return fetcher({\n url: urlJoin(\n appClientUrl,\n '/api/v1/plugins' satisfies EndpointPlugins['path'],\n ),\n body: {\n pluginId,\n type: 'getContent',\n } satisfies ExtractEndpointData<EndpointPlugins>,\n });\n}\n\nexport async function renderTemplate({\n options,\n appClientUrl,\n}: {\n options: PatternRenderData;\n appClientUrl: string;\n}): Promise<KsRenderResults> {\n const data = await fetcher<KsRenderResults>({\n url: urlJoin(appClientUrl, '/api/v1/render' satisfies EndpointPaths),\n body: options,\n });\n return data;\n}\n\nexport async function verifyFile({\n appClientUrl,\n payload,\n}: {\n appClientUrl: string;\n payload: ExtractEndpointData<EndpointFiles>['payload'];\n}) {\n const data = await fetcher<ExtractEndpointResBody<EndpointFiles>>({\n url: urlJoin(appClientUrl, '/api/v1/files' satisfies EndpointFiles['path']),\n body: {\n type: 'verify',\n payload,\n } satisfies ExtractEndpointData<EndpointFiles>,\n });\n return data.payload;\n}\n\nexport async function getDiscovery({\n appClientUrl,\n authHeader,\n}: {\n appClientUrl: string;\n authHeader: string;\n}): Promise<\n | {\n type: 'success';\n data: RouteDiscovery['output'];\n }\n | { type: 'routeNotAvailable' }\n> {\n try {\n const headers: Record<string, string> = {};\n if (authHeader) {\n headers.authorization = authHeader;\n }\n const data = await fetcher<RouteDiscovery['output']>({\n url: urlJoin(\n appClientUrl,\n '/api/v2/discovery' satisfies RouteDiscovery['path'],\n ),\n headers,\n });\n if (!data) {\n throw new Error('No result from discovery');\n }\n return {\n type: 'success',\n data,\n };\n } catch (error) {\n if (error instanceof FetcherError && error.status === 404) {\n return {\n type: 'routeNotAvailable',\n };\n }\n // being extra cautious here to avoid throwing an error\n return { type: 'routeNotAvailable' };\n }\n}\n\n/**\n * `/api/v2/renderer-discovery` for one renderer. Only for renderers the App\n * Client itself discovers; native mobile renderers are discovered by the App\n * API instead (`isRendererIdForNativeMobile`), so callers route those there.\n * Throws on any non-2xx answer, including the 401 a private site gives an\n * anonymous caller.\n */\nexport async function getRendererDiscovery<\n TheRendererId extends RendererId = RendererId,\n>({\n appClientUrl,\n rendererId,\n authHeader,\n}: {\n appClientUrl: string;\n rendererId: TheRendererId;\n authHeader: string;\n}): Promise<RouteRendererDiscovery<TheRendererId>['output']> {\n const headers: Record<string, string> = {};\n if (authHeader) {\n headers.authorization = authHeader;\n }\n return fetcher<RouteRendererDiscovery<TheRendererId>['output']>({\n url: urlJoin(\n appClientUrl,\n '/api/v2/renderer-discovery' satisfies RouteRendererDiscovery<TheRendererId>['path'],\n ),\n query: {\n rendererId,\n } satisfies RouteRendererDiscovery<TheRendererId>['input'],\n headers,\n });\n}\n\nexport async function inspectTemplate({\n appClientUrl,\n templateInfo,\n authHeader,\n}: {\n appClientUrl: string;\n templateInfo: RouteInspect['input'];\n authHeader: string;\n}): Promise<RouteInspect['output']> {\n const headers: Record<string, string> = {};\n if (authHeader) {\n headers.authorization = authHeader;\n }\n return fetcher({\n url: urlJoin(\n appClientUrl,\n '/api/v2/inspect' satisfies RouteInspect['path'],\n ),\n query: templateInfo,\n headers,\n });\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/get-template-suggestions.ts","../src/app-client-api.ts"],"names":["getTemplateSuggestions","appClientUrl","query","fetcher","__name","checkAppClientUrl","url","theUrl","urlJoin","err","getDataStore","getAppClientData","serverDataRes","message","prepAppClientData","state","metaState","appClientDataNoMeta","prepareDataForFileSave","urlBase","body","FetcherError","submitDataForFileSave","data","getPluginContent","pluginId","renderTemplate","options","verifyFile","payload","getDiscovery","authHeader","headers","error","getRendererDiscovery","rendererId","inspectTemplate","templateInfo","PKG_NOT_FOUND_MESSAGE_PREFIX","getClientErrorMessage","parsed","getPkgInfo","pkgName"],"mappings":"2EAKA,IAAA,CAAA,CAAA,MAAA,CAAA,cAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,GAAA,CAAA,CAAA,CAAA,CAAA,MAAA,CAAA,CAAA,KAAA,CAAA,CAAA,CAAA,YAAA,CAAA,IAAA,CAAA,CAAA,CAEA,eAAsBA,CAAAA,CAAuB,CAC3C,YAAA,CAAAC,CAAAA,CACA,KAAA,CAAAC,CACF,EAGwC,CACtC,OAAOC,QAAoC,CACzC,GAAA,CAAK,IAAI,GAAA,CACP,8BAAA,CACAF,CACF,CAAA,CAAE,UAAS,CACX,KAAA,CAAAC,CACF,CAAC,CACH,CAdsBE,CAAAA,CAAAJ,CAAAA,CAAA,wBAAA,CAAA,CCqBtB,eAAsBK,CAAAA,CAAkBC,EAAuC,CAC7E,GAAI,CACF,IAAMC,EAASC,CAAAA,CAAQF,CAAAA,CAAK,SAAiC,CAAA,CAI7D,OAHY,MAAMH,OAAAA,CAAyB,CACzC,GAAA,CAAKI,CACP,CAAC,CAEH,CAAA,MAASE,CAAAA,CAAK,CACZ,OAAO,CACL,EAAA,CAAI,KAAA,CACJ,OAAA,CAASA,aAAe,KAAA,CAAQA,CAAAA,CAAI,OAAA,CAAU,eAChD,CACF,CACF,CAbsBL,EAAAC,CAAAA,CAAA,mBAAA,CAAA,CAetB,eAAsBK,CAAAA,CAAa,CACjC,YAAA,CAAAT,CACF,EAEqD,CACnD,GAAI,CAKF,OAAO,CACL,EAAA,CAAI,IAAA,CACJ,IAAA,CANW,MAAME,QAAgC,CACjD,GAAA,CAAKK,EAAQP,CAAAA,CAAc,oBAA4C,CACzE,CAAC,CAKD,CACF,CAAA,MAASQ,EAAK,CACZ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,KAAA,CAAO,CAAE,QAAAyC,CAAQ,CAAA,CACjB,QAAAV,CACF,CAAC,CAC8B,CACjC,OAASC,CAAAA,CAAO,CACd,IAAMpB,CAAAA,CACJoB,aAAiBZ,YAAAA,EAAgBY,CAAAA,CAAM,MAAA,GAAW,GAAA,CAC9CM,EAAsBN,CAAAA,CAAM,YAAY,EACxC,MAAA,CACN,GAAIpB,IAAY,MAAA,CAAW,MAAMoB,CAAAA,CACjC,OAAIpB,EAAQ,UAAA,CAAWyB,CAA4B,CAAA,CAC1C,CAAE,KAAM,cAAe,CAAA,CAEzB,CAAE,IAAA,CAAM,gBAAiB,OAAA,CAAAzB,CAAQ,CAC1C,CACF,CAtCsBT,EAAAqC,CAAAA,CAAA,YAAA,CAAA","file":"index.mjs","sourcesContent":["import type {\n EndpointPaths,\n TemplateSuggestionsReqParams,\n TemplateSuggestionsResBody,\n} from '@knapsack/app/api-info';\nimport { fetcher } from '@knapsack/utils';\n\nexport async function getTemplateSuggestions({\n appClientUrl,\n query,\n}: {\n appClientUrl: string;\n query: TemplateSuggestionsReqParams;\n}): Promise<TemplateSuggestionsResBody> {\n return fetcher<TemplateSuggestionsResBody>({\n url: new URL(\n '/api/v1/template-suggestions' satisfies EndpointPaths,\n appClientUrl,\n ).toString(),\n query,\n });\n}\n","import {\n type KsAppClientData,\n type KsRenderResults,\n type Except,\n type GenericResponse,\n type KsAppClientDataAndMeta,\n type KsAppClientDataNoMeta,\n type KsFileSaverParams,\n type RendererId,\n} from '@knapsack/types';\nimport { fetcher, FetcherError } from '@knapsack/utils';\nimport urlJoin from 'url-join';\nimport type {\n PatternRenderData,\n KnapsackDataStoreSaveBody,\n EndpointPaths,\n EndpointFiles,\n ExtractEndpointData,\n ExtractEndpointResBody,\n EndpointPlugins,\n RouteDiscovery,\n RouteInspect,\n RoutePkgInfo,\n RouteRendererDiscovery,\n} from '@knapsack/app/api-info';\n\nexport type { PatternRenderData, EndpointPaths };\n\nexport async function checkAppClientUrl(url: string): Promise<GenericResponse> {\n try {\n const theUrl = urlJoin(url, '/api/v1' satisfies EndpointPaths);\n const res = await fetcher<GenericResponse>({\n url: theUrl,\n });\n return res;\n } catch (err) {\n return {\n ok: false,\n message: err instanceof Error ? err.message : 'Unknown error',\n };\n }\n}\n\nexport async function getDataStore({\n appClientUrl,\n}: {\n appClientUrl: string;\n}): Promise<GenericResponse<KsAppClientDataAndMeta>> {\n try {\n const data = await fetcher<KsAppClientDataAndMeta>({\n url: urlJoin(appClientUrl, '/api/v1/data-store' satisfies EndpointPaths),\n });\n\n return {\n ok: true,\n data,\n };\n } catch (err) {\n return {\n ok: false,\n message: err instanceof Error ? err.message : 'Unknown error',\n };\n }\n}\n\n// just need a function that can throw an error\nexport async function getAppClientData({\n appClientUrl,\n}: {\n appClientUrl: string;\n}): Promise<KsAppClientDataAndMeta> {\n const serverDataRes = await getDataStore({ appClientUrl });\n if (!serverDataRes.ok || !serverDataRes.data) {\n const message = `We're having trouble accessing your server at ${appClientUrl}.`;\n throw new Error(message);\n }\n return serverDataRes.data;\n}\n\n/** Sole job is to strip `metaState` if it's there */\nexport function prepAppClientData(\n state: KsAppClientData,\n): KsAppClientDataNoMeta {\n const { metaState, ...appClientDataNoMeta } = state;\n return appClientDataNoMeta;\n}\n\nexport async function prepareDataForFileSave({\n urlBase,\n state,\n}: {\n urlBase: string;\n state: KsAppClientDataNoMeta;\n}): Promise<GenericResponse<KsFileSaverParams>> {\n try {\n const body: KnapsackDataStoreSaveBody = {\n state: prepAppClientData(state),\n };\n const data = await fetcher<GenericResponse<KsFileSaverParams>>({\n url: urlJoin(urlBase, '/api/v1/data-store-prep' satisfies EndpointPaths),\n body,\n });\n\n return data;\n } catch (err) {\n if (err instanceof FetcherError) {\n // Preserve the upstream response so the caller can log the actual\n // headers/body of the failure instead of just \"Error 500\".\n return {\n ok: false,\n message: err.message,\n meta: {\n status: err.status,\n statusText: err.statusText,\n url: err.url,\n responseHeaders: err.responseHeaders,\n responseBody: err.responseBody,\n },\n };\n }\n return {\n ok: false,\n message: err instanceof Error ? err.message : 'Unknown error',\n };\n }\n}\n\nexport async function submitDataForFileSave({\n urlBase,\n state,\n}: {\n urlBase: string;\n state: KsAppClientDataNoMeta;\n}): Promise<void> {\n const appClientData = prepAppClientData(state);\n const body: KnapsackDataStoreSaveBody = {\n state: appClientData,\n };\n const data = await fetcher<GenericResponse>({\n url: urlJoin(urlBase, '/api/v1/data-store' satisfies EndpointPaths),\n body,\n });\n\n if (!data.ok) {\n throw new Error(`Could not save data to localhost files: ${data.message}`);\n }\n}\n\nexport async function getPluginContent({\n pluginId,\n appClientUrl,\n}: {\n appClientUrl: string;\n} & Except<ExtractEndpointData<EndpointPlugins>, 'type'>): Promise<\n ExtractEndpointResBody<EndpointPlugins>\n> {\n return fetcher({\n url: urlJoin(\n appClientUrl,\n '/api/v1/plugins' satisfies EndpointPlugins['path'],\n ),\n body: {\n pluginId,\n type: 'getContent',\n } satisfies ExtractEndpointData<EndpointPlugins>,\n });\n}\n\nexport async function renderTemplate({\n options,\n appClientUrl,\n}: {\n options: PatternRenderData;\n appClientUrl: string;\n}): Promise<KsRenderResults> {\n const data = await fetcher<KsRenderResults>({\n url: urlJoin(appClientUrl, '/api/v1/render' satisfies EndpointPaths),\n body: options,\n });\n return data;\n}\n\nexport async function verifyFile({\n appClientUrl,\n payload,\n}: {\n appClientUrl: string;\n payload: ExtractEndpointData<EndpointFiles>['payload'];\n}) {\n const data = await fetcher<ExtractEndpointResBody<EndpointFiles>>({\n url: urlJoin(appClientUrl, '/api/v1/files' satisfies EndpointFiles['path']),\n body: {\n type: 'verify',\n payload,\n } satisfies ExtractEndpointData<EndpointFiles>,\n });\n return data.payload;\n}\n\nexport async function getDiscovery({\n appClientUrl,\n authHeader,\n}: {\n appClientUrl: string;\n authHeader: string;\n}): Promise<\n | {\n type: 'success';\n data: RouteDiscovery['output'];\n }\n | { type: 'routeNotAvailable' }\n> {\n try {\n const headers: Record<string, string> = {};\n if (authHeader) {\n headers.authorization = authHeader;\n }\n const data = await fetcher<RouteDiscovery['output']>({\n url: urlJoin(\n appClientUrl,\n '/api/v2/discovery' satisfies RouteDiscovery['path'],\n ),\n headers,\n });\n if (!data) {\n throw new Error('No result from discovery');\n }\n return {\n type: 'success',\n data,\n };\n } catch (error) {\n if (error instanceof FetcherError && error.status === 404) {\n return {\n type: 'routeNotAvailable',\n };\n }\n // being extra cautious here to avoid throwing an error\n return { type: 'routeNotAvailable' };\n }\n}\n\n/**\n * `/api/v2/renderer-discovery` for one renderer. Only for renderers the App\n * Client itself discovers; native mobile renderers are discovered by the App\n * API instead (`isRendererIdForNativeMobile`), so callers route those there.\n * Throws on any non-2xx answer, including the 401 a private site gives an\n * anonymous caller.\n */\nexport async function getRendererDiscovery<\n TheRendererId extends RendererId = RendererId,\n>({\n appClientUrl,\n rendererId,\n authHeader,\n}: {\n appClientUrl: string;\n rendererId: TheRendererId;\n authHeader: string;\n}): Promise<RouteRendererDiscovery<TheRendererId>['output']> {\n const headers: Record<string, string> = {};\n if (authHeader) {\n headers.authorization = authHeader;\n }\n return fetcher<RouteRendererDiscovery<TheRendererId>['output']>({\n url: urlJoin(\n appClientUrl,\n '/api/v2/renderer-discovery' satisfies RouteRendererDiscovery<TheRendererId>['path'],\n ),\n query: {\n rendererId,\n } satisfies RouteRendererDiscovery<TheRendererId>['input'],\n headers,\n });\n}\n\nexport async function inspectTemplate({\n appClientUrl,\n templateInfo,\n authHeader,\n}: {\n appClientUrl: string;\n templateInfo: RouteInspect['input'];\n authHeader: string;\n}): Promise<RouteInspect['output']> {\n const headers: Record<string, string> = {};\n if (authHeader) {\n headers.authorization = authHeader;\n }\n return fetcher({\n url: urlJoin(\n appClientUrl,\n '/api/v2/inspect' satisfies RouteInspect['path'],\n ),\n query: templateInfo,\n headers,\n });\n}\n\n/**\n * The App Client's error-handler message when `resolvePkg` cannot find the\n * package (`tools/file-utils/src/pkg-utils.ts`). The client sends no status\n * or type for it, only a 500 with this text, so the prefix is the signal.\n */\nconst PKG_NOT_FOUND_MESSAGE_PREFIX = 'Could not find package \"';\n\n/** The `message` of the App Client error handler's `{ ok: false, message }` body. */\nfunction getClientErrorMessage(body: string | undefined): string | undefined {\n if (!body) return undefined;\n try {\n const parsed: unknown = JSON.parse(body);\n if (\n typeof parsed === 'object' &&\n parsed !== null &&\n 'ok' in parsed &&\n parsed.ok === false &&\n 'message' in parsed &&\n typeof parsed.message === 'string'\n ) {\n return parsed.message;\n }\n } catch {\n // Not the client's JSON: a proxy's error page.\n }\n return undefined;\n}\n\n/**\n * `/api/v2/pkg-info` for one package: its `package.json` as the App Client\n * resolves it from the site's own directory. A package the client cannot\n * resolve is `pkg.notFound`; any other 500 from the client's own error\n * handler (a `{ ok: false, message }` body) is `error.unknown` with that\n * message, which can name paths on the client's machine. Throws on every\n * other failure: a 401 from a private site, a 404 from a client without the\n * route, a 500 from a proxy in front of the client, a network error.\n */\nexport async function getPkgInfo({\n appClientUrl,\n pkgName,\n authHeader,\n}: {\n appClientUrl: string;\n pkgName: string;\n authHeader: string;\n}): Promise<\n | { type: 'success'; data: RoutePkgInfo['output'] }\n | { type: 'pkg.notFound' }\n | { type: 'error.unknown'; message: string }\n> {\n const headers: Record<string, string> = {};\n if (authHeader) {\n headers.authorization = authHeader;\n }\n try {\n const data = await fetcher<RoutePkgInfo['output']>({\n url: urlJoin(\n appClientUrl,\n '/api/v2/pkg-info' satisfies RoutePkgInfo['path'],\n ),\n query: { pkgName } satisfies RoutePkgInfo['input'],\n headers,\n });\n return { type: 'success', data };\n } catch (error) {\n const message =\n error instanceof FetcherError && error.status === 500\n ? getClientErrorMessage(error.responseBody)\n : undefined;\n if (message === undefined) throw error;\n if (message.startsWith(PKG_NOT_FOUND_MESSAGE_PREFIX)) {\n return { type: 'pkg.notFound' };\n }\n return { type: 'error.unknown', message };\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@knapsack/app-client-api",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.110.2",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -14,13 +14,13 @@
|
|
|
14
14
|
},
|
|
15
15
|
"sideEffects": false,
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@knapsack/app": "4.
|
|
18
|
-
"@knapsack/types": "4.
|
|
19
|
-
"@knapsack/utils": "4.
|
|
17
|
+
"@knapsack/app": "4.110.2",
|
|
18
|
+
"@knapsack/types": "4.110.2",
|
|
19
|
+
"@knapsack/utils": "4.110.2",
|
|
20
20
|
"url-join": "^4.0.1"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@knapsack/typescript-config-starter": "4.
|
|
23
|
+
"@knapsack/typescript-config-starter": "4.110.2",
|
|
24
24
|
"@types/node": "^24.13.2",
|
|
25
25
|
"@types/url-join": "^4.0.3",
|
|
26
26
|
"eslint": "^9.20.0",
|