Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: CI

on:
push:
branches: [main]
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 10
- uses: actions/setup-node@v4
with:
node-version: 22
cache: pnpm
- run: pnpm install --frozen-lockfile
# Registry copies are current, worker tests, then the SDK build with its size, SSR and type checks.
- run: pnpm test
- run: pnpm typecheck
6 changes: 3 additions & 3 deletions docs/_skill.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Lookup by Here's My Church is a church search box for forms. People type a few l

## Steps
1. Load the element once, near the end of the page or in the app's root layout:
`<script type="module" src="https://lookup.heresmychurch.com/element.js"></script>`
For a service-times editor, also load `https://lookup.heresmychurch.com/service-times.js`.
`<script type="module" src="https://lookup.heresmychurch.com/sdk/0.4.0/element.js"></script>`
For a service-times editor, also load `https://lookup.heresmychurch.com/sdk/0.4.0/service-times.js`.
2. Add the field inside the form, above the fields it fills:
`<church-lookup name="church_id" label="Church"></church-lookup>`
- `name`: the chosen church's ID submits with the form. Save it.
Expand All @@ -26,7 +26,7 @@ Lookup by Here's My Church is a church search box for forms. People type a few l
3. Inputs named for the church already fill: `church_city`, `churchWebsite`, `church[phone]`, or `autocomplete="section-church tel"`. Tag any other input with `data-hmc-field`. Valid names only:
name, street, city, region, regionName, postalCode, country, countryName, latitude, longitude, phone, email, website, denomination, serviceTimes, languages, ministries, pastorName, id.
On a form that's only about the church, `fill="auto"` also fills plain names (city, state, zip, phone, website). Empty fields fill; typed values are kept unless the element has `overwrite`.
4. Frameworks: it's a standard custom element. In React or Vue, attach `church-select` (detail.result.id), `church-clear` and `church-error` listeners in an effect. In Next.js, import `@heresmychurch/lookup/element` from a `"use client"` file. Inputs are filled through the native setter plus `input`/`change` events, so controlled inputs stay in sync. Types: `import type {} from "@heresmychurch/lookup/react"` or `/vue`.
4. Frameworks: it's a standard custom element. Listen for `church-select` (detail.result.id), `church-clear` and `church-error`: in React 19 as `onchurch-select` (and so on) props, in Vue as `@church-select`, in React 18 with addEventListener in an effect. In Next.js, import `@heresmychurch/lookup/element` from a `"use client"` file. Inputs are filled through the native setter plus `input`/`change` events, so controlled inputs stay in sync. Types: `import type {} from "@heresmychurch/lookup/react"` or `/vue`.
5. While testing, set `client="lookup-demo"`: shared corrections are checked but never sent. In production, set `client` to the app's name.
6. Styling: set `--hmc-accent`, `--hmc-radius`, `--hmc-border`, `--hmc-input-border`, `--hmc-bg`, `--hmc-text` on `church-lookup`, or rely on shadcn-style tokens (`--background`, `--foreground`, `--accent`, `--border`, `--radius`) already on the page.

Expand Down
17 changes: 10 additions & 7 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Add a church search field to the form in this project where people enter a churc

## Steps
1. Load the widget once on the page:
<script type="module" src="https://lookup.heresmychurch.com/element.js"></script>
(Service times editor, optional: https://lookup.heresmychurch.com/service-times.js)
<script type="module" src="https://lookup.heresmychurch.com/sdk/0.4.0/element.js"></script>
(Service times editor, optional: https://lookup.heresmychurch.com/sdk/0.4.0/service-times.js)
2. Put <church-lookup name="church_id" label="Church"></church-lookup> inside the form, above the fields it fills.
- `name` submits the chosen church's ID with the form. Store it so you can look the church up again.
- Add `required` if a church must be chosen.
Expand All @@ -21,8 +21,9 @@ Add a church search field to the form in this project where people enter a churc
name, street, city, region, regionName, postalCode, country, countryName, latitude, longitude,
phone, email, website, denomination, serviceTimes, languages, ministries, pastorName, id
Don't create new inputs unless I ask. Don't rename existing ones.
4. React or Vue: the element is a custom element. Listen for `church-select` and `church-clear` events
with addEventListener in an effect; values are set the way typing sets them, so controlled inputs stay in sync.
4. React or Vue: the element is a custom element. React 19: pass onchurch-select and onchurch-clear props.
React 18: add `church-select` and `church-clear` listeners with addEventListener in an effect.
Vue: @church-select and @church-clear. Values are set the way typing sets them, so controlled inputs stay in sync.
Types: import type {} from "@heresmychurch/lookup/react" (or /vue).
5. While testing, add client="lookup-demo" so corrections people share are checked but never sent.
Remove it (or set your app's name) before shipping.
Expand All @@ -41,9 +42,11 @@ Or set it up yourself. There's no key to request.
## 1. Add the script

```html
<script type="module" src="https://lookup.heresmychurch.com/element.js"></script>
<script type="module" src="https://lookup.heresmychurch.com/sdk/0.4.0/element.js"></script>
```

That URL is pinned to version 0.4.0 and never changes, so an update can't change your form until you change the number. To always get the latest release instead, load `https://lookup.heresmychurch.com/element.js`. See the [changelog](https://github.com/harvouscom/lookup/blob/main/packages/sdk/CHANGELOG.md) for what's in each version.

## 2. Put the search box in your form

Fields named for the church, like `church_city`, fill on their own. Tag any others with `data-hmc-field`. Give it a `name` and the chosen church's ID submits with the form.
Expand All @@ -57,10 +60,10 @@ Fields named for the church, like `church_city`, fill on their own. Tag any othe
<input name="website" data-hmc-field="website">
<button>Join</button>
</form>
<script type="module" src="https://lookup.heresmychurch.com/element.js"></script>
<script type="module" src="https://lookup.heresmychurch.com/sdk/0.4.0/element.js"></script>
```
```javascript title="client only"
import { createLookup } from "https://lookup.heresmychurch.com/lookup.js";
import { createLookup } from "https://lookup.heresmychurch.com/sdk/0.4.0/lookup.js";

const lookup = createLookup({ client: "my-app" });
const { results } = await lookup.search("grace comm");
Expand Down
2 changes: 2 additions & 0 deletions docs/react.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Add a church picker to this React + shadcn/ui app using Lookup by Here's My Chur
Check: type "grac", pick a church, the tagged fields fill and handleSubmit receives church_id.
:::

Not on shadcn/ui? Use the element with `onchurch-select` props in React 19 (see [the widget](/docs/widget#in-your-form)), or build your own picker with `useChurchLookup` from `@heresmychurch/lookup/react` once the package is on npm.

## Install

```bash
Expand Down
2 changes: 1 addition & 1 deletion docs/service-times.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Wrap a service-times field in `<church-service-times>` to edit it as rows of day
<church-service-times>
<textarea name="times" data-hmc-field="serviceTimes"></textarea>
</church-service-times>
<script type="module" src="https://lookup.heresmychurch.com/service-times.js"></script>
<script type="module" src="https://lookup.heresmychurch.com/sdk/0.4.0/service-times.js"></script>
```

When `<church-lookup>` fills a `serviceTimes` field inside `<church-service-times>`, the rows update with it.
31 changes: 28 additions & 3 deletions docs/widget.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ It also asks for smarter results when people pause. Change the options below to

## In your form

It's a real form field. Give it a `name` and the chosen church's ID submits with your form, so you can store the ID and look the church up again later. React and Vue get types for the tag and its events.
It's a real form field. Give it a `name` and the chosen church's ID submits with your form, so you can store the ID and look the church up again later. React and Vue get types for the tag and its events. React 19 adds `onchurch-select` and other `onchurch-*` props as event listeners; React 18 needs them added in an effect.

:::tabs Form example
```html title="html form"
Expand All @@ -86,10 +86,25 @@ It's a real form field. Give it a `name` and the chosen church's ID submits with
</form>
<!-- Posts church_id=CA-ON-56994145&city=Thorold -->
```
```tsx title="react"
```tsx title="react 19"
"use client"; // Next.js: load the element in a client component.
import "@heresmychurch/lookup/element";
import type {} from "@heresmychurch/lookup/react"; // types the props and onchurch-* events

function ChurchField({ onChange }) {
return (
<church-lookup
name="church_id"
lang="es"
onchurch-select={(e) => onChange(e.detail.result.id)}
onchurch-clear={() => onChange("")}
/>
);
}
```
```tsx title="react 18"
"use client"; // Next.js: load the element in a client component.
import "@heresmychurch/lookup/element";
import type {} from "@heresmychurch/lookup/react";

function ChurchField({ onChange }) {
const ref = useRef(null);
Expand All @@ -107,6 +122,16 @@ function ChurchField({ onChange }) {
return <church-lookup ref={ref} name="church_id" lang="es" />;
}
```
```vue title="vue"
<script setup lang="ts">
import "@heresmychurch/lookup/element";
import type {} from "@heresmychurch/lookup/vue";
</script>

<template>
<church-lookup name="church_id" @church-select="(e) => (churchId = e.detail.result.id)" @church-clear="churchId = ''" />
</template>
```
:::

## Fields
Expand Down
1 change: 1 addition & 0 deletions examples/shadcn-demo/src/hooks/use-church-lookup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client"

// Generated from packages/sdk/src/use-church-lookup.ts by scripts/build-registry.mjs. MIT.
// useChurchLookup: Lookup's search engine as a React hook, for building your own church picker.
// The debounce, settled search, "Show more", stale-result handling and record loading all live in
// @/lib/lookup-core, the same engine the <church-lookup> element uses.
Expand Down
93 changes: 84 additions & 9 deletions examples/shadcn-demo/src/lib/lookup-core.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
// Lookup by Here's My Church, headless core v0.3.0. Generated from @heresmychurch/lookup
// Lookup by Here's My Church, headless core v0.4.0. Generated from @heresmychurch/lookup
// (packages/sdk/src) by scripts/build-registry.mjs. MIT. https://lookup.heresmychurch.com/docs
/* eslint-disable */

// ── dev.ts ──
// Setup hints for developers. Each prints once, and only on a local development host (localhost,
// 127.0.0.1, *.localhost, *.test), so production consoles and servers stay quiet.
const said: Record<string, 1> = {};

export function devWarn(message: string): void {
const host = typeof location == "object" ? location.hostname : "";
if (said[message] || !/^(localhost|127\.0\.0\.1|\[::1\])$|\.(localhost|test)$/.test(host)) return;
said[message] = 1;
console.warn("Lookup: " + message);
}

// ── client.ts ──
// @heresmychurch/lookup core client. No dependencies.

Expand Down Expand Up @@ -56,13 +68,58 @@ export interface SearchResponse {
attribution: string;
}

/** One weekly service. */
export interface ServiceSlot {
/** 0 is Sunday, 6 is Saturday. */
dayOfWeek: number;
/** 24-hour "HH:MM", such as "09:30". */
startTime: string;
}

export interface ServiceTimes {
/** The listed text, such as "Sun 9:00 AM; Wed 7:00 PM", or null when there is none. */
raw: string | null;
/** The services read from `raw`. Empty when it couldn't be read; `raw` still holds the text. */
slots: ServiceSlot[];
/** Whether every part of `raw` was read into `slots`. */
parsed: boolean;
}

/** A church's details. Every key is always present, with null or [] when the directory has nothing. */
export interface ChurchFields {
name: string;
street: string | null;
city: string | null;
/** A region code, such as "TX" or "ON". */
region: string;
/** The region's name, such as "Texas". */
regionName: string;
postalCode: string | null;
/** An ISO country code, such as "US". */
country: string;
countryName: string;
latitude: number;
longitude: number;
phone: string | null;
email: string | null;
website: string | null;
denomination: string | null;
serviceTimes: ServiceTimes;
languages: string[];
ministries: string[];
pastorName: string | null;
}

/** A field name in a church record, as data-hmc-field takes. */
export type ChurchField = keyof ChurchFields | "id";

export interface ChurchRecord {
id: string;
/** Only with { follow: false }: this listing merged into another. Load that ID instead. */
/** Only with { follow: false }: this listing merged into another. Load that ID instead; nothing else is set. */
movedTo?: string;
/** The ID you asked for, when it merged into this church. Store this record's `id` instead. */
movedFrom?: string;
fields: Record<string, unknown> & { name: string; serviceTimes: { raw: string | null } };
fields: ChurchFields;
missing: string[];
completeness: number;
/** When someone last confirmed the listing (ms since 1970), or null if never. */
Expand Down Expand Up @@ -102,7 +159,7 @@ export class LookupError extends Error {
status: number;
/** Seconds to wait before trying again, on a 429 or 503. */
retryAfter?: number;
/** A stable reason: invalid, not_found, rate_limited, overloaded, unavailable, … */
/** A stable reason: invalid, not_found, rate_limited, overloaded, unavailable, timeout, … */
code?: string;
constructor(status: number, message: string, retryAfter?: number, code?: string) {
super(message);
Expand All @@ -118,17 +175,35 @@ export interface LookupOptions {
/** Your tool's name, such as "my-chms". Self-declared; shown to reviewers as declared. */
client?: string;
fetch?: typeof fetch;
/** Give up on a request after this many milliseconds, with a LookupError whose code is "timeout". Defaults to 15000; 0 waits forever. */
timeout?: number;
}

export function createLookup(opts: LookupOptions = {}) {
const base = (opts.baseUrl ?? "https://lookup.heresmychurch.com").replace(/\/+$/, "") + "/v1";
const f = opts.fetch ?? fetch.bind(globalThis);
const timeout = opts.timeout ?? 15000;
if (!opts.client) devWarn('set client="my-app" so Here\'s My Church knows which tool is asking. Use "lookup-demo" while testing.');

async function get<T>(path: string, signal?: AbortSignal, init?: RequestInit): Promise<T> {
const res = await f(base + path, { signal, ...init });
const body = await res.json().catch(() => ({}));
if (!res.ok) throw new LookupError(res.status, body.error ?? res.statusText, Number(res.headers.get("retry-after")) || undefined, body.code);
return body;
// One signal for the caller's abort and the timeout. A caller's abort still rejects with AbortError.
const ctl = new AbortController();
const stop = () => ctl.abort();
const timer = timeout > 0 ? setTimeout(stop, timeout) : 0;
if (signal?.aborted) stop();
signal?.addEventListener("abort", stop);
try {
const res = await f(base + path, { ...init, signal: ctl.signal });
const body = await res.json().catch(() => ({}));
if (!res.ok) throw new LookupError(res.status, body.error ?? res.statusText, Number(res.headers.get("retry-after")) || undefined, body.code);
return body;
} catch (e) {
// Aborted, but not by the caller: the timeout fired.
throw ctl.signal.aborted && !signal?.aborted ? new LookupError(0, "Timed out", undefined, "timeout") : e;
} finally {
clearTimeout(timer);
signal?.removeEventListener("abort", stop);
}
}

const post = <T>(path: string, body: unknown, keepalive?: boolean) =>
Expand Down Expand Up @@ -370,7 +445,7 @@ export const MAX_RESULTS = 50;

/** A record field as the text a form field should hold, or null when the directory has nothing. */
export function fieldValue(record: ChurchRecord, field: string): string | null {
const v = field === "id" ? record.id : record.fields[field];
const v = field === "id" ? record.id : (record.fields as unknown as Record<string, unknown>)[field];
if (v == null) return null;
if (Array.isArray(v)) return v.length ? v.join(", ") : null;
if (typeof v === "object") return (v as { raw?: string | null }).raw ?? null;
Expand Down
33 changes: 33 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Changelog

All notable changes to `@heresmychurch/lookup`. Versions follow [semver](https://semver.org); while the version is 0.x, a minor release can change behavior, so pin the exact version you test against.

## 0.4.0

- React: `useChurchLookup` ships in `@heresmychurch/lookup/react`, so React apps without shadcn/ui get the headless core as a hook. React is an optional peer dependency, needed only for that entry.
- React 19 types for `<church-lookup>`: `onchurch-select`, `onchurch-fill` and the other event props are typed (React 19 adds them as listeners), the usual HTML props come from React's own types, and the object properties `fieldMap`, `strings` and `lookup` are typed. Breaking, types only: an unknown camelCase prop such as `maxResults` is now a type error instead of passing silently. Use `max-results`.
- Vue: `@church-select` and the other events are typed in templates. Preact and React 18 (global JSX) get typed `on<event>` props too.
- CommonJS build: every entry has a `.cjs` file and `.d.cts` types behind a `require` condition, so Jest and other `require()` setups load the package with no transforms.
- Client `timeout` option: a request with no answer after 15 seconds (by default) fails with a `LookupError` whose `code` is `"timeout"`. A caller's own abort still rejects with `AbortError`.
- Setup hints: on a local development host, the console says once when `client` isn't set, or when a chosen church found no fields to fill. Production consoles stay quiet.
- The headless core's size budget is now 5.5 KB gzipped (it was 5 KB), for the timeout and setup hints.
- Types: `ChurchRecord.fields` is now a typed `ChurchFields`, so `record.fields.city` is `string | null` and `record.fields.languages` is `string[]` with no casts. New exported types: `ChurchFields`, `ChurchField`, `ServiceTimes` and `ServiceSlot`.
- Types resolve under every TypeScript `moduleResolution`, including the older `"node"` (node10) that many existing apps and Jest setups use.
- Pinned script URLs: each release is also served at `https://lookup.heresmychurch.com/sdk/<version>/element.js` (and `lookup.js`, `service-times.js`), cached for a year and never changed. The unversioned URLs keep tracking the latest release.
- Package metadata for npm: repository, homepage, keywords, license file and public access.

## 0.3.0

- "Show more" in the results, with `limit` and `max-results`.
- Inputs named for the church (`church_city`, `churchWebsite`, `autocomplete="section-church tel"`) fill with no tags. `fill="auto"` also fills plain names like `city`; `@heresmychurch/lookup/fields` exposes the matcher.
- New attributes: `density`, `max-results`, `min-chars`, `debounce`, `lat`, `lng`, `semantic="off"`, `show-distance="off"`, `fill`. New `church-error` event.
- Sharing fixes back is checked by default; "Still correct" shows only for stale listings; report closed, duplicate or moved; add a church that isn't listed (`church-add`, `church-added`).
- Type declarations ship compiled, so apps need no special TypeScript settings.

## 0.2.0

- `@heresmychurch/lookup/core`: the headless controller, fill planning and consent rules, with no side effects on import.
- `<church-lookup>` is a real form field: `name` submits the church ID, `value` prefills it, `required` validates, and form reset and restore work.
- Spanish, French, German, Portuguese and Polish text, loaded on demand.
- Types for React, Vue, Preact and Solid.
- The shadcn/ui registry: `ChurchLookup`, `useChurchLookup` and `ChurchConsent`.
Loading
Loading