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

HTTP API fallback, without wrangler #11

Open
nicolas-albert opened this issue Nov 16, 2023 · 2 comments
Open

HTTP API fallback, without wrangler #11

nicolas-albert opened this issue Nov 16, 2023 · 2 comments
Labels
wontfix This will not be worked on

Comments

@nicolas-albert
Copy link

Hi,
I want to use CF and D1 for building a project using SvelteKit and its prerender feature.

Prerender is done at build time with vanilla node, not wrangler, so no bindings are available.
But HTTP requests are allowed and we can call APIs with HTTP.
I'll do this because the Query API is simple.
I also need D1 at runtime so I can use D1 bindings in this case.

But I wondering if this great wrangler-proxy project can perform API calls directly on the D1 HTTP endpoint, by providing in environment variables kind of CF_ACCOUNT_ID, CF_D1_DB_ID, CF_D1_DB_TOKEN.

For now, I just need simple query and I have no time to contribute but maybe one day.
Thx.

@nicolas-albert
Copy link
Author

nicolas-albert commented Nov 17, 2023

I've done something like this (for a sveltekit project) :

import { CF_ACCOUNT_ID, CF_D1_ID, CF_D1_TOKEN } from '$env/static/private';

/** @type {D1Database | undefined | null} */
let _d1 = undefined;

/** @param {D1Database | undefined} d1  */
export async function setD1(d1) {
	if (_d1 != undefined) {
		return;
	}
	_d1 = null;
	try {
		let res = await d1?.exec('SELECT 1');
		if (res?.count == 1) {
			_d1 = d1;
		}
	} catch (_) {}
}

/**
 *
 * @param {string} sql
 * @param {string[]} params
 * @returns
 */
export async function d1Query(sql, params = []) {
	let json;
	if (_d1) {
		json = await _d1.prepare(sql).bind(params).all();
	} else {
		const res = await fetch(
			`https://api.cloudflare.com/client/v4/accounts/${CF_ACCOUNT_ID}/d1/database/${CF_D1_ID}/query`,
			{
				method: 'POST',
				body: JSON.stringify({ sql, params }),
				headers: {
					Authorization: `Bearer ${CF_D1_TOKEN}`,
					'Content-Type': 'application/json'
				}
			}
		);
		json = await res.json();
	}
	return json;
}

@chientrm
Copy link
Owner

I've done something like this (for a sveltekit project) :

import { CF_ACCOUNT_ID, CF_D1_ID, CF_D1_TOKEN } from '$env/static/private';

/** @type {D1Database | undefined | null} */
let _d1 = undefined;

/** @param {D1Database | undefined} d1  */
export async function setD1(d1) {
	if (_d1 != undefined) {
		return;
	}
	_d1 = null;
	try {
		let res = await d1?.exec('SELECT 1');
		if (res?.count == 1) {
			_d1 = d1;
		}
	} catch (_) {}
}

/**
 *
 * @param {string} sql
 * @param {string[]} params
 * @returns
 */
export async function d1Query(sql, params = []) {
	let json;
	if (_d1) {
		json = await _d1.prepare(sql).bind(params).all();
	} else {
		const res = await fetch(
			`https://api.cloudflare.com/client/v4/accounts/${CF_ACCOUNT_ID}/d1/database/${CF_D1_ID}/query`,
			{
				method: 'POST',
				body: JSON.stringify({ sql, params }),
				headers: {
					Authorization: `Bearer ${CF_D1_TOKEN}`,
					'Content-Type': 'application/json'
				}
			}
		);
		json = await res.json();
	}
	return json;
}

That looks like a Workers API client wrapper tbh. However I wouldn't implement it in wrangler-proxy 🤔 , maybe you can start a new project 👍

@chientrm chientrm added the wontfix This will not be worked on label Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants