wix-storybook-utils
Version:
Utilities for automated component documentation within Storybook
47 lines (40 loc) • 1.13 kB
text/typescript
const defaultSnippetDatastoreUrl = `https://www.wix.com/_serverless/wix-style-react-playground/snippet`;
export const loadSnippet = async (
snippetId: string,
snippetDatastoreUrl = defaultSnippetDatastoreUrl,
) => {
try {
const response = await fetch(`${snippetDatastoreUrl}/${snippetId}`);
const { isSafe, code } = await response.json();
if (code && code.trim().length) {
return {
code,
isSafe,
};
}
return Promise.reject(`Invalid code snippet loaded: ${code}`);
} catch (error) {
return Promise.reject(error);
}
};
export const saveSnippet = async (
code: string,
snippetDatastoreUrl = defaultSnippetDatastoreUrl,
): Promise<string> => {
try {
const response = await fetch(snippetDatastoreUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ data: code }),
});
if (response.status === 200) {
const { id } = await response.json();
return id;
}
return Promise.reject(response.statusText);
} catch (error) {
return Promise.reject(error);
}
};