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

Use .ts file as config file? #1379

Open
m1212e opened this issue Oct 24, 2024 · 3 comments
Open

Use .ts file as config file? #1379

m1212e opened this issue Oct 24, 2024 · 3 comments

Comments

@m1212e
Copy link

m1212e commented Oct 24, 2024

Describe the feature

Posting this here because I reached the 2000 char Discord limit and the discussion section of this repo is disabled.

I would like to import my scalar File serializer:

export interface FileScalar {
	base64EncodedArrayBuffer: string;
	type: string;
	fileName: string;
	size: number;
	lastModified: number;
}

function isFileScalar(file: unknown): file is FileScalar {
	return (
		typeof file === 'object' &&
		file !== null &&
		'base64EncodedArrayBuffer' in file &&
		'type' in file &&
		'fileName' in file &&
		'size' in file &&
		'lastModified' in file
	);
}

export async function encodeFileScalar(file: File): Promise<FileScalar> {
	return {
		base64EncodedArrayBuffer: btoa(
			String.fromCharCode(...new Uint8Array(await file.arrayBuffer()))
		),
		type: file.type,
		fileName: file.name,
		size: file.size,
		lastModified: file.lastModified
	};
}

export function decodeFileScalar(file: FileScalar | unknown): File {
	if (!isFileScalar(file)) {
		throw new Error('Invalid file scalar type: ' + JSON.stringify(file));
	}
	return new File([atob(file.base64EncodedArrayBuffer)], file.fileName, {
		type: file.type,
		lastModified: file.lastModified
	});
}

in my houdini config:

import { decodeFileScalar, encodeFileScalar } from './src/api/services/fileScalar.ts';

/// <references types="houdini-svelte">

/** @type {import('houdini').ConfigFile} */
const config = {
	watchSchema: {
		url: 'http://localhost:5173/api/graphql'
	},
	plugins: {
		'houdini-svelte': {
			forceRunesMode: true
		}
	},
	scalars: {
		DateTime: {
			type: 'Date',
			unmarshal(val) {
				return val ? new Date(val) : null;
			},
			marshal(date) {
				return date && date.getTime();
			}
		},
		File: {
			type: 'File',
			unmarshal: decodeFileScalar,
			marshal: encodeFileScalar
		}
	}
};

export default config;

which doesn't work:

Error: Could not load config file at file://[...]houdini.config.js.
Unknown file extension ".ts" for [...]/fileScalar.ts

Is this somehow possible? I'd like to prevent copying over code since changes in serialization won't automatically adapt!
In my server side pothos builder I can simply add:

builder.scalarType('File', {
	serialize: encodeFileScalar,
	parseValue: decodeFileScalar
});

Criticality

nice to have

@AlecAivazis
Copy link
Collaborator

Yea it's not (yet) supported since the config file has to be imported by a node process. It would be possible to support by detecting the typescript config file and compiling it to javascript before we run our processes but I haven't found the time to implement it. If this is something you want to try, let me know and I'd gladly offer whatever guidance you need

@m1212e
Copy link
Author

m1212e commented Oct 27, 2024

I very much see that point. I'm gonna give this a shot in the next weeks or so and I'll get back to you before opening a PR. Thanks for the quick response!

@AlecAivazis
Copy link
Collaborator

Awesome! If you want faster response times, you can always reach out to me on discord

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants