SvelteKit
ActiveServer-side validation in +page.server.ts; client widget mounted in onMount.
Install
Client widget
npm i @postio/address-finder
Server validation
npm i @postio/node
30-second example
// src/routes/checkout/+page.server.ts
import { Postio } from '@postio/node';
import { POSTIO_API_KEY } from '$env/static/private';
const postio = new Postio({ apiKey: POSTIO_API_KEY });
export const actions = {
validate: async ({ request }) => {
const data = await request.formData();
const postcode = String(data.get('postcode') ?? '');
const { results } = await postio.address.postcode(postcode);
return { results };
},
};
// src/routes/checkout/+page.svelte
<script lang="ts">
import { onMount } from 'svelte';
import { setup } from '@postio/address-finder';
import { PUBLIC_POSTIO_KEY } from '$env/static/public';
let inputEl: HTMLInputElement;
onMount(() => setup({
apiKey: PUBLIC_POSTIO_KEY,
input: inputEl,
output: {
address_line_1: '#line1',
post_town: '#town',
postcode: '#postcode',
},
}));
</script>
<input bind:this={inputEl} placeholder="Start typing an address…" />
<input id="line1" />
<input id="town" />
<input id="postcode" />