0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

feat: always return serve urls

This commit is contained in:
Princesseuh 2024-05-21 13:18:28 +02:00
parent 000372bd4a
commit e4546de377
No known key found for this signature in database
GPG key ID: 105BBD6D57F2B0C0
4 changed files with 70 additions and 54 deletions

View file

@ -61,8 +61,11 @@ const fileType = customType<{ data: string; driverData: string }>({
return value;
},
fromDriver(value) {
// TODO: Should return a file object
return value;
// TODO: Get this from somewhere else
const studioUrl =
process.env.ASTRO_STUDIO_FILE_SERVER || 'https://studio.astro.build/api/serve_file';
return `${studioUrl}?id=${value}`;
},
});

View file

@ -1,9 +1,13 @@
import type { AstroConfig } from 'astro';
import { existsSync } from 'node:fs';
import { mkdir, writeFile } from 'node:fs/promises';
import { getAstroStudioStorageUrl } from './utils.js';
import {
getAstroStudioFileServerUrl,
getProjectIdFromFile,
getSessionIdFromFile,
} from '@astrojs/studio';
import type { AstroConfig } from 'astro';
import { STORAGE_CODE_FILE, STORAGE_TYPES_FILE } from './consts.js';
import { getProjectIdFromFile, getSessionIdFromFile } from '@astrojs/studio';
import { getAstroStudioStorageUrl } from './utils.js';
export async function codegen(astroConfig: Pick<AstroConfig, 'root'>) {
await codegenInternal({ root: astroConfig.root });
@ -11,9 +15,10 @@ export async function codegen(astroConfig: Pick<AstroConfig, 'root'>) {
async function codegenInternal({ root }: { root: URL }) {
const dotAstroDir = new URL('.astro/', root);
const serveUrl = getAstroStudioFileServerUrl();
const images = await storageRequest('image');
const all = await storageRequest('all');
const images = await storageRequest('image', serveUrl);
const all = await storageRequest('all', serveUrl);
const code = `
// This file is auto-generated by Astro Studio. Do not modify this file directly.
@ -30,7 +35,7 @@ async function codegenInternal({ root }: { root: URL }) {
export function getFile(name: import("./${STORAGE_CODE_FILE}").File);
export function getStudioImage(name: import("./${STORAGE_CODE_FILE}").Image);
}
`
`;
if (!existsSync(dotAstroDir)) {
await mkdir(dotAstroDir);
@ -40,7 +45,7 @@ async function codegenInternal({ root }: { root: URL }) {
await writeFile(new URL(STORAGE_TYPES_FILE, dotAstroDir), types);
}
async function storageRequest(fileKind: 'all' | 'image' = 'all') {
async function storageRequest(fileKind: 'all' | 'image' = 'all', serveUrl: string) {
const projectId = await getProjectIdFromFile();
const linkUrl = getAstroStudioStorageUrl();
const sessionToken = await getSessionIdFromFile();
@ -62,13 +67,13 @@ async function storageRequest(fileKind: 'all' | 'image' = 'all') {
}
);
return groupDataByName((await response.json()).data);
return rearrangeData((await response.json()).data, serveUrl);
}
function groupDataByName(data: any) {
function rearrangeData(data: any, serveUrl: string) {
const grouped: Record<string, any> = {};
for (const item of data) {
grouped[item.name] = item;
grouped[item.name] = { ...item, serve_url: `${serveUrl}?id=${item.id}` };
}
return grouped;

View file

@ -1,12 +1,12 @@
import type { AstroConfig, AstroIntegration, AstroIntegrationLogger } from "astro";
import { STORAGE_CODE_FILE, STORAGE_TYPES_FILE } from "./consts.js";
import { codegen } from "./codegen.js";
import { readFile, writeFile } from "node:fs/promises";
import { bold } from "kleur/colors";
import path from "node:path";
import { fileURLToPath } from "url";
import { normalizePath } from "vite";
import { existsSync } from "node:fs";
import { existsSync } from 'node:fs';
import { readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'url';
import type { AstroConfig, AstroIntegration, AstroIntegrationLogger } from 'astro';
import { bold } from 'kleur/colors';
import { normalizePath } from 'vite';
import { codegen } from './codegen.js';
import { STORAGE_CODE_FILE, STORAGE_TYPES_FILE } from './consts.js';
const VIRTUAL_MODULE_ID = 'astro:storage';
const RESOLVED_MODULE_ID = '\0' + 'astro:storage';
@ -29,33 +29,36 @@ function vitePluginStorage({ root }: { root: URL }): VitePlugin {
import { images, all } from '${new URL(STORAGE_CODE_FILE, dotAstroDir)}';
export function getFile(name) {
return all[name];
return all[name].serve_url;
}
export function getStudioImage(name) {
return images[name].id;
}
`
}
return images[name].serve_url;
}
`;
},
};
}
export function storageIntegration(): AstroIntegration {
return {
name: "astro:studio",
return {
name: 'astro:studio',
hooks: {
'astro:config:setup': async ({ config, updateConfig, logger }) => {
updateConfig({
vite: {
plugins: [vitePluginStorage({ root: config.root }), vitePluginInjectEnvTs({ srcDir: config.srcDir, root: config.root }, logger)]
}
})
plugins: [
vitePluginStorage({ root: config.root }),
vitePluginInjectEnvTs({ srcDir: config.srcDir, root: config.root }, logger),
],
},
});
},
'astro:config:done': async ({ config }) => {
await codegen({ root: config.root });
}
}
}
},
},
};
}
export function vitePluginInjectEnvTs(

View file

@ -9,3 +9,8 @@ export function getAstroStudioUrl(): string {
const env = getAstroStudioEnv();
return env.ASTRO_STUDIO_URL || 'https://studio.astro.build';
}
export function getAstroStudioFileServerUrl(): string {
const env = getAstroStudioEnv();
return env.ASTRO_STUDIO_FILE_SERVER || 'https://studio.astro.build/api/serve_file';
}