diff --git a/.changeset/client-spec-0-3-0.md b/.changeset/client-spec-0-3-0.md new file mode 100644 index 0000000..96f9dff --- /dev/null +++ b/.changeset/client-spec-0-3-0.md @@ -0,0 +1,5 @@ +--- +'@gatepost/client': patch +--- + +The package implements spec 0.3.0. That version adds the postcode field and its messages, and leaves the client contract as it was, so every call behaves as before. diff --git a/.changeset/core-spec-0-3-0.md b/.changeset/core-spec-0-3-0.md new file mode 100644 index 0000000..1ee4996 --- /dev/null +++ b/.changeset/core-spec-0-3-0.md @@ -0,0 +1,5 @@ +--- +'@gatepost/core': patch +--- + +The package implements spec 0.3.0. That version adds the postcode field and its messages, and leaves the grammar as it was, so every function gives the same results as before. diff --git a/.changeset/first-field-release.md b/.changeset/first-field-release.md new file mode 100644 index 0000000..6ca9e11 --- /dev/null +++ b/.changeset/first-field-release.md @@ -0,0 +1,5 @@ +--- +'@gatepost/field': minor +--- + +Add the custom element `gatepost-postcode-field`. A plain HTML form submits the canonical form of the postcode. The field checks the format offline, offers the fix of a look-alike character, accepts or refuses a legacy postcode, and works with the keyboard alone. With a publishable key, it asks NIPOST's gateway about each whole postcode and names its place. A failed or negative answer never stops the form. The field refuses a secret key and sends no request with it. With `gps` and a key, it fills in the postcode of the user's location, or only the part that the accuracy supports, when the user presses its button. diff --git a/.changeset/first-react-release.md b/.changeset/first-react-release.md new file mode 100644 index 0000000..7662a25 --- /dev/null +++ b/.changeset/first-react-release.md @@ -0,0 +1,5 @@ +--- +'@gatepost/react': minor +--- + +Add the React package. Its component renders the postcode field of `@gatepost/field` on the server and in the browser, for React 18 and 19, and calls a prop for each event of the field. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9917feb..156b8c5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,6 +33,8 @@ jobs: enable-cache: false - run: corepack enable - run: pnpm install --frozen-lockfile + # The tests of the field run in Chromium. + - run: pnpm exec playwright install --with-deps chromium - run: pnpm check node-22: @@ -48,8 +50,42 @@ jobs: node-version: 22 - run: corepack enable - run: pnpm install --frozen-lockfile + - run: pnpm exec playwright install --with-deps chromium - run: pnpm test + # pnpm check runs the journeys of the field in Chromium. This job runs them in the other two + # engines, so a page in Firefox or Safari gets the same field. + browsers: + runs-on: ubuntu-24.04 + timeout-minutes: 15 + strategy: + fail-fast: false + matrix: + browser: [firefox, webkit] + env: + BROWSER: ${{ matrix.browser }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + submodules: true + persist-credentials: false + - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0 + with: + node-version: 24 + - run: corepack enable + - run: pnpm install --frozen-lockfile + - run: pnpm exec playwright install --with-deps "$BROWSER" + - run: pnpm build + - run: pnpm --filter @gatepost/field exec playwright test --project="$BROWSER" + # A failure in an engine that nobody runs by hand needs its trace. + - if: failure() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: traces-${{ matrix.browser }} + path: packages/field/test-results/ + if-no-files-found: ignore + retention-days: 7 + commits: if: github.event_name == 'pull_request' runs-on: ubuntu-24.04 diff --git a/.gitignore b/.gitignore index 2ec3672..e04eda5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,8 @@ dist/ coverage/ temp/ reports/ +test-results/ +playwright-report/ .stryker-tmp/ *.tsbuildinfo +.vitest/ diff --git a/.prettierignore b/.prettierignore index f53dad2..c4ee747 100644 --- a/.prettierignore +++ b/.prettierignore @@ -5,4 +5,5 @@ spec **/temp **/etc/*.api.md **/spec-data.ts +**/spec-messages.ts **/CHANGELOG.md diff --git a/AGENTS.md b/AGENTS.md index 2f12651..aa4b4d8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,6 +1,6 @@ # AGENTS.md -This repo is part of Gatepost, unofficial open-source developer tools for Nigeria's National Digital Postcode. This repo holds the TypeScript packages. Today it has `@gatepost/core`, `@gatepost/client` and the private `@gatepost/mock-server`. +This repo is part of Gatepost, unofficial open-source developer tools for Nigeria's National Digital Postcode. This repo holds the TypeScript packages. Today it has `@gatepost/core`, `@gatepost/client`, `@gatepost/field`, `@gatepost/react` and the private `@gatepost/mock-server`. ## Read first @@ -10,7 +10,7 @@ This repo is part of Gatepost, unofficial open-source developer tools for Nigeri ## Rules that no config file shows -- If the task needs a public symbol that the Interface section of `spec/grammar.md` does not list, add nothing, not even an internal helper. Stop, and say that a spec change must come first (API-1, CS-8). +- If the task needs a public symbol that no Interface section of the spec lists, in `spec/grammar.md`, `spec/client.md` or `spec/field.md`, add nothing, not even an internal helper. Stop, and say that a spec change must come first (API-1, CS-8). - Call only the documented NIPOST gateway endpoints. - Commit only NIPOST's published test codes and synthetic values, such as `FC-01-Z99-ZZ-01`. - Sign off each commit with `git commit -s`. The person who opens the pull request is the only author. Leave out co-author lines for AI tools. diff --git a/CONTEXT.md b/CONTEXT.md index fcc8182..4b82f5f 100644 --- a/CONTEXT.md +++ b/CONTEXT.md @@ -169,6 +169,18 @@ _Avoid_: input, widget, picker To show the user what a postcode points to before the app stores it. _Avoid_: verify, validate (for this step) +**Check status**: +What the WooCommerce plugin learned about the postcode of an order: `valid`, `invalid`, `unchecked` or `error`. The order meta `_gatepost_postcode_check` holds it. +_Avoid_: verification status, validation result + +**Form value**: +The text that a field gives its form when the form is sent: the canonical form of a postcode, the 6 digits of a legacy postcode, or the text without white space at the start and the end. +_Avoid_: submitted value, output + +**Message catalogue**: +A data file that holds the text of a UI in one language, one message for each key. +_Avoid_: strings file, translations, locale file + **Tell**: A habit that makes code look machine-written, such as very long lines. _Avoid_: smell (smells are design problems), anti-pattern diff --git a/README.md b/README.md index e240310..13cbacd 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ | ----------------------------------------------- | ----------------------------------------------- | -------------------------- | | [`@gatepost/core`](packages/core) | Parse, check and format postcodes offline. | Alpha, not on npm yet | | [`@gatepost/client`](packages/client) | Call NIPOST's gateway, with retries and limits. | Alpha, not on npm yet | +| [`@gatepost/field`](packages/field) | A postcode field for any web form. | Alpha, not on npm yet | +| [`@gatepost/react`](packages/react) | The postcode field as a React component. | Alpha, not on npm yet | | [`@gatepost/mock-server`](packages/mock-server) | Mock NIPOST's gateway for client tests. | Private, never goes to npm | ## Example diff --git a/docs/develop.md b/docs/develop.md index a07c581..f231c62 100644 --- a/docs/develop.md +++ b/docs/develop.md @@ -8,8 +8,13 @@ You need Node 24, `corepack enable`, Python 3.11 or later and `uv`. git clone --recurse-submodules https://github.com/gatepost-dev/js pnpm install + pnpm exec playwright install chromium pnpm check +The tests of the field and of the React package run in Chromium, which Playwright installs once. +`pnpm check` runs the journeys of the field too, with a test page on port 3000 and the mock server +on port 4010, so both ports must be free. + `pnpm check` also builds the packages. Then it runs publint, attw, size-limit and API Extractor on the result, and it starts each built package once. It checks the licence of each dependency and of each file, and it checks the workflows with zizmor. It starts REUSE and zizmor with `uvx` at pinned diff --git a/eslint.config.js b/eslint.config.js index 31dec61..ee88aa3 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -28,6 +28,7 @@ export default defineConfig( '**/.stryker-tmp/**', 'spec/**', '**/spec-data.ts', + '**/spec-messages.ts', ], }, eslint.configs.recommended, @@ -90,6 +91,8 @@ export default defineConfig( }, ], 'jsdoc/tag-lines': ['error', 'any', { startLines: 1 }], + // A getter documents a property, so a @returns tag would only repeat its summary. + 'jsdoc/require-returns': ['error', { checkGetters: false }], 'jsdoc/check-tag-names': [ 'error', { definedTags: ['packageDocumentation', 'internal', 'defaultValue'] }, diff --git a/package.json b/package.json index a0fdd48..3ef4474 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "scripts": { "check": "run-s check:source check:build check:policy", "check:source": "run-s generate:check format:check lint typecheck test", - "check:build": "run-s build package:check", + "check:build": "run-s build package:check test:browser", "check:policy": "run-s tells context:check check:licences workflows:check", "check:licences": "run-s licences:check licence-files:check reuse:check", "format": "prettier --write .", @@ -17,6 +17,7 @@ "lint": "eslint . --max-warnings 0", "typecheck": "pnpm -r run typecheck", "test": "pnpm -r run test && node --test scripts/*.test.mjs", + "test:browser": "pnpm -r run test:browser", "build": "pnpm -r run build", "package:check": "pnpm -r run package:check", "tells": "python3 spec/scripts/check-tells", @@ -25,27 +26,34 @@ "licence-files:check": "pnpm -r run licence-files:check", "reuse:check": "uvx --with charset-normalizer==3.5.2 reuse@6.2.0 lint", "workflows:check": "uvx zizmor@1.30.1 --offline .github/workflows", - "generate": "node scripts/generate-spec-data.mjs", - "generate:check": "node scripts/generate-spec-data.mjs --check" + "generate": "node scripts/generate-spec-data.mjs && node scripts/generate-messages.mjs", + "generate:check": "run-s generate:check:*", + "generate:check:data": "node scripts/generate-spec-data.mjs --check", + "generate:check:messages": "node scripts/generate-messages.mjs --check" }, "packageManager": "pnpm@12.8.1", "devDependencies": { "@arethetypeswrong/cli": "^0.18.5", + "@axe-core/playwright": "^4.13.0", "@changesets/cli": "^3.0.3", "@commitlint/cli": "^21.2.3", "@commitlint/config-conventional": "^21.2.3", "@eslint-community/eslint-plugin-eslint-comments": "^4.8.1", "@eslint/js": "^10.0.1", "@microsoft/api-extractor": "^7.59.3", + "@playwright/test": "^1.63.0", "@size-limit/preset-small-lib": "^14.1.0", "@stryker-mutator/core": "^10.0.0", "@types/node": "^22.20.4", + "@vitest/browser-playwright": "^5.0.3", "@vitest/coverage-v8": "^5.0.3", "ajv": "^8.20.0", + "axe-core": "^4.13.0", "eslint": "^10.11.0", "eslint-plugin-jsdoc": "^65.0.1", "fast-check": "^4.10.2", "npm-run-all2": "^9.0.3", + "playwright": "^1.63.0", "prettier": "^3.9.9", "publint": "^0.3.24", "size-limit": "^14.1.0", diff --git a/packages/client/README.md b/packages/client/README.md index 873cff1..d7ed2f6 100644 --- a/packages/client/README.md +++ b/packages/client/README.md @@ -109,7 +109,7 @@ The client also takes a `transport` option, a function like `fetch`, and each ca | ------------- | ---------------------------------------------- | | Node | 22 or later, or a current browser with `fetch` | | Module format | ESM only | -| Gatepost spec | 0.2.0 | +| Gatepost spec | 0.3.0 | ## Docs diff --git a/packages/client/etc/client.api.md b/packages/client/etc/client.api.md index 352b142..bd0c4bd 100644 --- a/packages/client/etc/client.api.md +++ b/packages/client/etc/client.api.md @@ -113,6 +113,6 @@ export interface ReverseUnit { } // @public -export const SPEC_VERSION = "0.2.0"; +export const SPEC_VERSION = "0.3.0"; ``` diff --git a/packages/client/src/version.ts b/packages/client/src/version.ts index 6051b64..49cad0c 100644 --- a/packages/client/src/version.ts +++ b/packages/client/src/version.ts @@ -8,7 +8,7 @@ * ```ts * import { SPEC_VERSION } from '@gatepost/client'; * - * SPEC_VERSION; // '0.2.0' + * SPEC_VERSION; // '0.3.0' * ``` */ -export const SPEC_VERSION = '0.2.0'; +export const SPEC_VERSION = '0.3.0'; diff --git a/packages/core/README.md b/packages/core/README.md index dc23b14..26766e0 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -107,7 +107,7 @@ The package checks the form of a postcode. It cannot tell whether a building has | ------------- | ----------------------------------- | | Node | 22 or later, or any current browser | | Module format | ESM only | -| Gatepost spec | 0.2.0 | +| Gatepost spec | 0.3.0 | ## Docs diff --git a/packages/core/etc/core.api.md b/packages/core/etc/core.api.md index ef7b226..6482ed6 100644 --- a/packages/core/etc/core.api.md +++ b/packages/core/etc/core.api.md @@ -68,7 +68,7 @@ export interface Segments { } // @public -export const SPEC_VERSION = "0.2.0"; +export const SPEC_VERSION = "0.3.0"; // @public export function stateName(stateCode: string): string | null; diff --git a/packages/core/src/version.ts b/packages/core/src/version.ts index ab60c2f..df3a3ce 100644 --- a/packages/core/src/version.ts +++ b/packages/core/src/version.ts @@ -8,7 +8,7 @@ * ```ts * import { SPEC_VERSION } from '@gatepost/core'; * - * SPEC_VERSION; // '0.2.0' + * SPEC_VERSION; // '0.3.0' * ``` */ -export const SPEC_VERSION = '0.2.0'; +export const SPEC_VERSION = '0.3.0'; diff --git a/packages/field/LICENSE b/packages/field/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/packages/field/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/packages/field/NOTICE b/packages/field/NOTICE new file mode 100644 index 0000000..bc94510 --- /dev/null +++ b/packages/field/NOTICE @@ -0,0 +1,5 @@ +Gatepost +Copyright 2026 The Gatepost authors + +This product is unofficial. The Nigerian Postal Service (NIPOST) did not make it and does not endorse it. +Gatepost uses the name NIPOST only to describe compatibility with NIPOST's National Digital Postcode. diff --git a/packages/field/README.md b/packages/field/README.md new file mode 100644 index 0000000..985b370 --- /dev/null +++ b/packages/field/README.md @@ -0,0 +1,179 @@ +

+ + + Gatepost + +

+ +

@gatepost/field

+ +

A postcode field for any web form. It checks Nigeria's new postcode while the user types.

+ +

+ CI status + Licence: Apache-2.0 + OpenSSF Scorecard +

+ +

+ Docs +  ·  + Playground +  ·  + Field spec +  ·  + Contributing +  ·  + Discussions +

+ +> Unofficial. Not made or endorsed by NIPOST. + +## Install + +```sh +pnpm add @gatepost/field +``` + +The first alpha is not on npm yet, so this command and the CDN address below do not work today. + +Import `@gatepost/field` once in your app, and the page can use the element. A page with no build step can load the one-file build from a CDN instead, as the quickstart does. Once a release exists, pin its version in the CDN address of a production page. + +## Quickstart + +```html + +
+ + +
+``` + +The element is empty until the script runs, so the layout moves when it loads, and a page with no JavaScript sends no postcode. A `