Skip to content

Commit

Permalink
Merge pull request #207 from jerriclynsjohn/axiom-svelte
Browse files Browse the repository at this point in the history
Fixing Sveltekit package and example issues
  • Loading branch information
dasfmi authored Jul 8, 2024
2 parents 5b1ff7c + 95e8f1c commit 5af4479
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 24 deletions.
2 changes: 1 addition & 1 deletion examples/svelte-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"dependencies": {
"@axiomhq/js": "1.0.0-rc.3",
"@axiomhq/svelte": "workspace:*"
"@axiomhq/sveltekit": "workspace:*"
},
"type": "module"
}
2 changes: 1 addition & 1 deletion examples/svelte-app/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<body data-sveltekit-preload-data="off">
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
29 changes: 29 additions & 0 deletions examples/svelte-app/src/hooks.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { HandleClientError } from '@sveltejs/kit';

import { Logger } from '@axiomhq/sveltekit';

import { dev, browser, version } from '$app/environment'


const logger = new Logger({
args: { dev, browser, version }
});


export const handleError: HandleClientError = async ({ error, event, status, message }) => {

const url = new URL(event.url);
console.log({ url })
logger.error(`${message}`, {
exception: error, request: {
host: url.hostname,
path: url.pathname,
status: status
},
source: 'hooks',
});

await logger.flush()

return { message }
}
24 changes: 13 additions & 11 deletions examples/svelte-app/src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import type { Handle, HandleServerError } from '@sveltejs/kit';

import { resolveRuntime, withAxiom, Logger } from '@axiomhq/sveltekit';

import { AXIOM_TOKEN, AXIOM_DATASET, AXIOM_URL } from '$env/static/private';
import { resolveRuntime, withAxiom, Logger } from '@axiomhq/svelte';
import { dev, browser, version } from '$app/environment'
import type { Handle } from '@sveltejs/kit';


console.log({ token: AXIOM_TOKEN, dataset: AXIOM_DATASET, url: AXIOM_URL })
const logger = new Logger({ token: AXIOM_TOKEN, dataset: AXIOM_DATASET, url: AXIOM_URL }, { runtime: resolveRuntime(), dev, browser, version });
const logger = new Logger({
dataset: AXIOM_DATASET,
token: AXIOM_TOKEN,
url: AXIOM_URL,
runtime: resolveRuntime(),
args: { dev, browser, version }
});


// /** @type {import('@sveltejs/kit').Handle} */
export const handle: Handle = withAxiom(logger, async ({ event, resolve }) => {
console.log('hooked')
return resolve(event);
})


export async function handleError({ error, event, status, message }) {
// console.error({ error, status, message })
export const handleError: HandleServerError = async ({ error, event, status, message }) => {

const url = new URL(event.request.url);
logger.error(`${message}`, {
exception: error, request: {
Expand All @@ -26,9 +31,6 @@ export async function handleError({ error, event, status, message }) {
status: status
},
source: 'hooks',
dev,
version,
browser,
});

await logger.flush()
Expand Down
6 changes: 6 additions & 0 deletions examples/svelte-app/src/routes/Header.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
<li aria-current={$page.url.pathname.startsWith('/sverdle') ? 'page' : undefined}>
<a href="/sverdle">Sverdle</a>
</li>
<li aria-current={$page.url.pathname.startsWith('/error-sample') ? 'page' : undefined}>
<a href="/error-sample">Error</a>
</li>
<li aria-current={$page.url.pathname.startsWith('/client-error-sample') ? 'page' : undefined}>
<a href="/client-error-sample">Client Error</a>
</li>
</ul>
<svg viewBox="0 0 2 3" aria-hidden="true">
<path d="M0,0 L0,3 C0.5,3 0.5,3 1,2 L2,0 Z" />
Expand Down
14 changes: 7 additions & 7 deletions examples/svelte-app/src/routes/api/axiom/+server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { AXIOM_TOKEN, AXIOM_DATASET } from '$env/static/private';
import type { RequestHandler } from '@sveltejs/kit';

import { AxiomWithoutBatching } from '@axiomhq/js'
import { PUBLIC_AXIOM_URL } from '$env/static/public';

const axiom = new AxiomWithoutBatching({ token: AXIOM_TOKEN, url: PUBLIC_AXIOM_URL });
import { AXIOM_TOKEN, AXIOM_DATASET, AXIOM_URL } from '$env/static/private';

const axiom = new AxiomWithoutBatching({ token: AXIOM_TOKEN, url: AXIOM_URL });

/** @type {import('./$types').RequestHandler} */
export async function POST({ url, platform, request }) {
console.log({ url, platform })
export const POST: RequestHandler = async ({ request }) => {

const events = await request.json()
axiom.ingest(AXIOM_DATASET, events)


return new Response("OK");
}
5 changes: 5 additions & 0 deletions examples/svelte-app/src/routes/api/error/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { RequestHandler } from '@sveltejs/kit';

export const GET: RequestHandler = async () => {
throw new Error(" Example API Route Error");
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div>
<main>
<p>
If it works
</p>
</main>
</div>
5 changes: 5 additions & 0 deletions examples/svelte-app/src/routes/client-error-sample/+page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { PageLoad } from "../$types";

export const load: PageLoad = async () => {
throw new Error(" Example Page Load Error");
};
14 changes: 14 additions & 0 deletions examples/svelte-app/src/routes/error-sample/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script lang="ts">
async function failure() {
const res = await fetch('/api/error');
if (!res.ok) {
throw new Error('Example Frontend Error');
}
}
</script>

<div>
<main>
<button type="button" on:click|preventDefault={failure}> Throw error! </button>
</main>
</div>
2 changes: 1 addition & 1 deletion packages/sveltekit/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const isEdgeRuntime = globalThis.EdgeRuntime ? true : false;

export function resolveRuntime() {
if (isWebWorker) {
return 'webworker';
return 'worker';
} else if (isEdgeRuntime) {
return 'edge';
} else if (typeof window !== 'undefined') {
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5af4479

Please sign in to comment.