SvelteKit
ActiveServer-side validation in +page.server.ts; client widget mounted in onMount.
SET UP
Install
Client widget
npm i @postio/address-finderServer validation
npm i @postio/nodeEXAMPLE
30-second example
ts
// 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" />
RELATED