Skip to content

Commit 9ecf32d

Browse files
authored
Add generic type parameter to FlashMessage component (#418)
* fix: release plan workflow * feat: make FlashMessage component generic for type-safe custom fields
1 parent a0ef113 commit 9ecf32d

13 files changed

Lines changed: 252 additions & 128 deletions

File tree

.github/workflows/plan-release.yml

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,58 @@
1-
name: Release Plan Review
1+
name: Plan Release
22
on:
3+
workflow_dispatch:
34
push:
45
branches:
56
- main
67
- master
7-
pull_request:
8+
pull_request_target: # This workflow has permissions on the repo, do NOT run code from PRs in this workflow. See https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
89
types:
910
- labeled
11+
- unlabeled
1012

1113
concurrency:
1214
group: plan-release # only the latest one of these should ever be running
1315
cancel-in-progress: true
1416

1517
jobs:
16-
check-plan:
17-
name: "Check Release Plan"
18+
should-run-release-plan-prepare:
19+
name: Should we run release-plan prepare?
1820
runs-on: ubuntu-latest
1921
outputs:
20-
command: ${{ steps.check-release.outputs.command }}
21-
22+
should-prepare: ${{ steps.should-prepare.outputs.should-prepare }}
2223
steps:
23-
- uses: actions/checkout@v4
24+
- uses: release-plan/actions/should-prepare-release@v1
2425
with:
25-
fetch-depth: 0
2626
ref: 'master'
27-
# This will only cause the `check-plan` job to have a "command" of `release`
28-
# when the .release-plan.json file was changed on the last commit.
29-
- id: check-release
30-
run: if git diff --name-only HEAD HEAD~1 | grep -w -q ".release-plan.json"; then echo "command=release"; fi >> $GITHUB_OUTPUT
27+
id: should-prepare
3128

32-
prepare_release_notes:
33-
name: Prepare Release Notes
29+
create-prepare-release-pr:
30+
name: Create Prepare Release PR
3431
runs-on: ubuntu-latest
3532
timeout-minutes: 5
36-
needs: check-plan
33+
needs: should-run-release-plan-prepare
3734
permissions:
3835
contents: write
36+
issues: read
3937
pull-requests: write
40-
outputs:
41-
explanation: ${{ steps.explanation.outputs.text }}
42-
# only run on push event if plan wasn't updated (don't create a release plan when we're releasing)
43-
# only run on labeled event if the PR has already been merged
44-
if: (github.event_name == 'push' && needs.check-plan.outputs.command != 'release') || (github.event_name == 'pull_request' && github.event.pull_request.merged == true)
45-
38+
if: needs.should-run-release-plan-prepare.outputs.should-prepare == 'true'
4639
steps:
47-
- uses: actions/checkout@v4
48-
# We need to download lots of history so that
49-
# github-changelog can discover what's changed since the last release
40+
- uses: release-plan/actions/prepare@v1
41+
name: Run release-plan prepare
5042
with:
51-
fetch-depth: 0
5243
ref: 'master'
53-
- uses: actions/setup-node@v4
54-
with:
55-
node-version: 18
56-
57-
- uses: pnpm/action-setup@v3
58-
with:
59-
version: 8
60-
- run: pnpm install --frozen-lockfile
61-
62-
- name: "Generate Explanation and Prep Changelogs"
63-
id: explanation
64-
run: |
65-
set +e
66-
67-
pnpm release-plan prepare 2> >(tee -a stderr.log >&2)
68-
69-
70-
if [ $? -ne 0 ]; then
71-
echo 'text<<EOF' >> $GITHUB_OUTPUT
72-
cat stderr.log >> $GITHUB_OUTPUT
73-
echo 'EOF' >> $GITHUB_OUTPUT
74-
else
75-
echo 'text<<EOF' >> $GITHUB_OUTPUT
76-
jq .description .release-plan.json -r >> $GITHUB_OUTPUT
77-
echo 'EOF' >> $GITHUB_OUTPUT
78-
fi
7944
env:
8045
GITHUB_AUTH: ${{ secrets.GITHUB_TOKEN }}
46+
id: explanation
8147

82-
- uses: peter-evans/create-pull-request@v6
48+
- uses: peter-evans/create-pull-request@v8
49+
name: Create Prepare Release PR
8350
with:
84-
commit-message: "Prepare Release using 'release-plan'"
51+
commit-message: "Prepare Release ${{ steps.explanation.outputs.new-version}} using 'release-plan'"
8552
labels: "internal"
53+
sign-commits: true
8654
branch: release-preview
87-
title: Prepare Release
55+
title: Prepare Release ${{ steps.explanation.outputs.new-version }}
8856
body: |
8957
This PR is a preview of the release that [release-plan](https://github.com/embroider-build/release-plan) has prepared. To release you should just merge this PR 👍
9058

.github/workflows/publish.yml

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
# For every push to the master branch, this checks if the release-plan was
2-
# updated and if it was it will publish stable npm packages based on the
3-
# release plan
1+
# For every push to the primary branch with .release-plan.json modified,
2+
# runs release-plan.
43

54
name: Publish Stable
65

@@ -10,53 +9,33 @@ on:
109
branches:
1110
- main
1211
- master
12+
paths:
13+
- '.release-plan.json'
1314

1415
concurrency:
1516
group: publish-${{ github.head_ref || github.ref }}
1617
cancel-in-progress: true
1718

1819
jobs:
19-
check-plan:
20-
name: "Check Release Plan"
21-
runs-on: ubuntu-latest
22-
outputs:
23-
command: ${{ steps.check-release.outputs.command }}
24-
25-
steps:
26-
- uses: actions/checkout@v4
27-
with:
28-
fetch-depth: 0
29-
ref: 'master'
30-
# This will only cause the `check-plan` job to have a result of `success`
31-
# when the .release-plan.json file was changed on the last commit. This
32-
# plus the fact that this action only runs on main will be enough of a guard
33-
- id: check-release
34-
run: if git diff --name-only HEAD HEAD~1 | grep -w -q ".release-plan.json"; then echo "command=release"; fi >> $GITHUB_OUTPUT
35-
3620
publish:
3721
name: "NPM Publish"
3822
runs-on: ubuntu-latest
39-
needs: check-plan
40-
if: needs.check-plan.outputs.command == 'release'
4123
permissions:
4224
contents: write
43-
pull-requests: write
25+
id-token: write
26+
attestations: write
4427

4528
steps:
46-
- uses: actions/checkout@v4
47-
- uses: actions/setup-node@v4
29+
- uses: actions/checkout@v6
30+
- uses: pnpm/action-setup@v4
31+
- uses: actions/setup-node@v6
4832
with:
49-
node-version: 18
50-
# This creates an .npmrc that reads the NODE_AUTH_TOKEN environment variable
33+
node-version: 22
5134
registry-url: 'https://registry.npmjs.org'
52-
53-
- uses: pnpm/action-setup@v3
54-
with:
55-
version: 8
35+
cache: pnpm
36+
- run: npm install -g npm@latest # ensure that the globally installed npm is new enough to support OIDC
5637
- run: pnpm install --frozen-lockfile
57-
- name: npm publish
58-
run: pnpm release-plan publish
59-
38+
- name: Publish to NPM
39+
run: NPM_CONFIG_PROVENANCE=true pnpm release-plan publish
6040
env:
6141
GITHUB_AUTH: ${{ secrets.GITHUB_TOKEN }}
62-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This ember addon adds a flash message service and component to your app.
2929
- [TypeScript](#typescript)
3030
- [Basic Usage](#basic-usage)
3131
- [Custom Fields with Generics](#custom-fields-with-generics)
32+
- [Typing Dynamically Registered Methods](#typing-dynamically-registered-methods)
3233
- [Displaying flash messages](#displaying-flash-messages)
3334
- [Custom `close` action](#custom-close-action)
3435
- [Styling with Foundation or Bootstrap](#styling-with-foundation-or-bootstrap)
@@ -499,6 +500,58 @@ this.flashMessages.success('Oops', {
499500
});
500501
```
501502

503+
Custom fields are also type-safe in templates. The `FlashMessage` component exposes the properly typed flash object with your custom fields:
504+
505+
```gjs
506+
import { FlashMessage } from 'ember-cli-flash';
507+
508+
<template>
509+
{{#each this.flashMessages.queue as |flash|}}
510+
<FlashMessage @flash={{flash}} as |component flash close|>
511+
{{flash.message}}
512+
{{flash.category}} {{! ✓ Typed as 'system' | 'user' | 'background' | undefined }}
513+
{{#if flash.action}}
514+
<button type="button" {{on "click" flash.action}}>Action</button>
515+
{{/if}}
516+
</FlashMessage>
517+
{{/each}}
518+
</template>
519+
```
520+
521+
### Typing Dynamically Registered Methods
522+
523+
When you configure custom `types` in `flashMessageDefaults`, the service dynamically creates convenience methods for each type at runtime. The base class already declares types for the default methods (`success`, `info`, `warning`, `danger`, `alert`, `secondary`), but TypeScript doesn't automatically recognize any custom types you add.
524+
525+
To get type safety for custom type methods, declare them explicitly in your service subclass:
526+
527+
```typescript
528+
import { FlashMessagesService } from 'ember-cli-flash';
529+
import type { FlashObjectOptions } from 'ember-cli-flash';
530+
531+
interface CustomFlashFields {
532+
id?: string;
533+
category?: string;
534+
}
535+
536+
type Options = FlashObjectOptions & CustomFlashFields;
537+
538+
export default class MyFlashMessages extends FlashMessagesService<CustomFlashFields> {
539+
// Only declare custom types not in the base class
540+
// (success, info, warning, danger, alert, secondary are already typed)
541+
declare error: (message: string, options?: Options) => this;
542+
declare custom: (message: string, options?: Options) => this;
543+
544+
get flashMessageDefaults() {
545+
return {
546+
...super.flashMessageDefaults,
547+
types: ['error', 'success', 'warning', 'custom'],
548+
};
549+
}
550+
}
551+
```
552+
553+
This pattern uses TypeScript's `declare` keyword to inform the type system about methods that exist at runtime but aren't defined in the base class types.
554+
502555
## Displaying flash messages
503556

504557
Then, to display somewhere in your app, add this to your component:

RELEASE.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ Releases in this repo are mostly automated using [release-plan](https://github.c
44

55
## Preparation
66

7-
Since the majority of the actual release process is automated, the remaining tasks before releasing are:
7+
Since the majority of the actual release process is automated, the remaining tasks before releasing are:
88

9-
- correctly labeling **all** pull requests that have been merged since the last release
10-
- updating pull request titles so they make sense to our users
9+
- correctly labeling **all** pull requests that have been merged since the last release
10+
- updating pull request titles so they make sense to our users
1111

1212
Some great information on why this is important can be found at [keepachangelog.com](https://keepachangelog.com/en/1.1.0/), but the overall
1313
guiding principle here is that changelogs are for humans, not machines.
1414

1515
When reviewing merged PR's the labels to be used are:
1616

17-
* breaking - Used when the PR is considered a breaking change.
18-
* enhancement - Used when the PR adds a new feature or enhancement.
19-
* bug - Used when the PR fixes a bug included in a previous release.
20-
* documentation - Used when the PR adds or updates documentation.
21-
* internal - Internal changes or things that don't fit in any other category.
17+
- breaking - Used when the PR is considered a breaking change.
18+
- enhancement - Used when the PR adds a new feature or enhancement.
19+
- bug - Used when the PR fixes a bug included in a previous release.
20+
- documentation - Used when the PR adds or updates documentation.
21+
- internal - Internal changes or things that don't fit in any other category.
2222

2323
**Note:** `release-plan` requires that **all** PRs are labeled. If a PR doesn't fit in a category it's fine to label it as `internal`
2424

UPGRADING.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,14 @@ Register custom types for your application:
132132
```typescript
133133
// app/services/flash-messages.ts
134134
import { FlashMessagesService } from 'ember-cli-flash';
135+
import type { FlashObjectOptions } from 'ember-cli-flash';
135136

136137
export default class FlashMessages extends FlashMessagesService {
138+
// Declare custom types for TypeScript (base types like success, warning are already typed)
139+
declare notice: (message: string, options?: FlashObjectOptions) => this;
140+
declare error: (message: string, options?: FlashObjectOptions) => this;
141+
declare system: (message: string, options?: FlashObjectOptions) => this;
142+
137143
get flashMessageDefaults() {
138144
return {
139145
...super.flashMessageDefaults,
@@ -247,6 +253,17 @@ const flash = this.flashMessages.findBy('id', 'save-notification');
247253
this.flashMessages.removeBy('userId', 123);
248254
```
249255

256+
The `FlashMessage` component is also generic and infers the type from the `@flash` arg, giving you type-safe access to custom fields in templates:
257+
258+
```gjs
259+
{{#each this.flashMessages.queue as |flash|}}
260+
<FlashMessage @flash={{flash}} as |component flash close|>
261+
{{flash.message}}
262+
{{flash.actionUrl}} {{! ✓ Typed as string | undefined }}
263+
</FlashMessage>
264+
{{/each}}
265+
```
266+
250267
### New Methods: `findBy` and `removeBy`
251268

252269
Two new methods have been added to the service for finding and removing flash messages by any field:

demo-app/components/demo-examples.gts

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ export default class DemoExamples extends Component {
7676
});
7777
};
7878

79+
// Custom typed methods (declared in service)
80+
showError = () => {
81+
this.flashMessages.error('Something went wrong!', {
82+
category: 'system',
83+
});
84+
};
85+
86+
showNotice = () => {
87+
this.flashMessages.notice('Did you know? This is a custom notice type.', {
88+
timeout: 5000,
89+
});
90+
};
91+
7992
// Custom fields with generics
8093
showWithCustomFields = () => {
8194
this.flashMessages.success('Message with custom fields', {
@@ -319,17 +332,35 @@ export default class DemoExamples extends Component {
319332
<p class="text-muted small mb-3">
320333
Use
321334
<code>add()</code>
322-
with any Bootstrap alert type or your own custom types
335+
with any Bootstrap alert type, or declare custom types in your service
323336
</p>
324-
<button
325-
type="button"
326-
class="btn btn-secondary"
327-
{{on "click" this.showCustomType}}
328-
>
329-
Show Secondary Type
330-
</button>
331-
<pre><code>this.flashMessages.add({ message: 'Custom type message', type:
332-
'secondary', timeout: 4000, });</code></pre>
337+
<div class="btn-group-example mb-2">
338+
<button
339+
type="button"
340+
class="btn btn-secondary"
341+
{{on "click" this.showCustomType}}
342+
>
343+
Secondary (via add)
344+
</button>
345+
<button
346+
type="button"
347+
class="btn btn-danger"
348+
{{on "click" this.showError}}
349+
>
350+
Error (custom type)
351+
</button>
352+
<button
353+
type="button"
354+
class="btn btn-light"
355+
{{on "click" this.showNotice}}
356+
>
357+
Notice (custom type)
358+
</button>
359+
</div>
360+
<pre><code>// Custom types declared in service: // declare error:
361+
(message: string, options?: Options) => this;
362+
this.flashMessages.error('Something went wrong!');
363+
this.flashMessages.notice('Did you know?');</code></pre>
333364
</section>
334365

335366
{{! TypeScript Generics }}

0 commit comments

Comments
 (0)