Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@clarksandholtz/no strip snack prefix #3

Open
wants to merge 3 commits into
base: @bycedric/snack-runtime
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions runtime/src/Files.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ export const list = () => Object.keys(files);
* Reset the current project files to only include the files from object.
* This is useful to manually load the files from our API instead of PubNub.
*/
export function updateProjectFiles(newFiles: Record<string, { type: 'CODE' | 'ASSET', contents: string }>) {
export function updateProjectFiles(
newFiles: Record<string, { type: 'CODE' | 'ASSET'; contents: string }>
) {
for (const filePath in files) {
delete files[filePath];
}
Expand All @@ -174,4 +176,4 @@ export function updateProjectFiles(newFiles: Record<string, { type: 'CODE' | 'AS
contents: !isAsset ? newFile.contents : undefined,
};
}
};
}
8 changes: 5 additions & 3 deletions runtime/src/utils/ExpoApiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ export type SnackApiCode = {
sdkVersion: string;
description: string;
dependencies: Record<string, string>;
}
}
};
};

export async function fetchCodeBySnackIdentifier(snackIdentifier: string): Promise<SnackApiCode | null> {
export async function fetchCodeBySnackIdentifier(
snackIdentifier: string
): Promise<SnackApiCode | null> {
try {
const res = await fetch(`https://exp.host/--/api/v2/snack/${snackIdentifier}`, {
method: 'GET',
Expand Down
25 changes: 16 additions & 9 deletions runtime/src/utils/SnackUrlUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export function extractChannelFromSnackUrl(url: string): string | null {
return matches ? matches[2] : null;
}

const snackPrefix = '@snack/';
const snackDomains = [
'http://snack.expo.dev/',
'https://snack.expo.dev/',
Expand All @@ -30,26 +29,34 @@ const snackDomains = [

export function extractSnackIdentifierFromSnackUrl(urlString: string): string | null {
try {
const snackDomain = snackDomains.find(domain => urlString.startsWith(domain));
const snackDomain = snackDomains.find((domain) => urlString.startsWith(domain));
if (!snackDomain) {
return null;
}

const pathWithoutSlash = urlString.substring(snackDomain.length);

let identifier = pathWithoutSlash;
if (identifier.includes('+')) { // check for a channel id
identifier = identifier.split('+')[0]
}
if (identifier.startsWith(snackPrefix)) {
identifier = identifier.slice(snackPrefix.length)
if (identifier.includes('+')) {
// check for a channel id
identifier = identifier.split('+')[0];
}
return identifier;
} catch {
return null;
}
}

export function createSnackUrlFromHashId(hashId: string): string {
return `https://exp.host/@snack/${hashId}`;
export function createSnackUrlFromSnackIdentifier(snackIdentifier: string): string {
return snackIdentifier.startsWith('@')
? `https://exp.host/${snackIdentifier}`
: `https://exp.host/@snack/${snackIdentifier}`;
}

export function isEphemeralSnackUrl(snackUrl: string): boolean {
if (snackUrl == null) {
return false;
}
const matches = snackUrl.match(channelRegex);
return matches != null && matches[1].length > 1;
}