Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow setting a custom Redacted.toString #3966

Open
wants to merge 3 commits into
base: next-minor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
allow providing a custom name to Redacted
  • Loading branch information
jessekelly881 committed Nov 20, 2024
commit 9ed1f160242c739048aec1988b147c6ebf2cf5ea
13 changes: 13 additions & 0 deletions .changeset/short-monkeys-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"effect": minor
---

allow users to provide a custom name for Redacted values on creation

```ts
Redacted.make("1234567890", "API_KEY")
```

example logged output:

level=INFO msg="permission granted" user=Perry token=API_KEY
4 changes: 2 additions & 2 deletions packages/effect/src/Redacted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ export const isRedacted: (u: unknown) => u is Redacted<unknown> = redacted_.isRe
* @example
* import { Redacted } from "effect"
*
* const API_KEY = Redacted.make("1234567890")
* const API_KEY = Redacted.make("1234567890", "API_KEY")
*
* @since 3.3.0
* @category constructors
*/
export const make: <A>(value: A) => Redacted<A> = redacted_.make
export const make: <A>(value: A, str?: string) => Redacted<A> = redacted_.make

/**
* Retrieves the original value from a `Redacted` instance. Use this function
Expand Down
25 changes: 14 additions & 11 deletions packages/effect/src/internal/redacted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ export const proto = {
pipe() {
return pipeArguments(this, arguments)
},
toString() {
return "<redacted>"
},
toJSON() {
return "<redacted>"
},
[NodeInspectSymbol]() {
return "<redacted>"
},
[Hash.symbol]<T>(this: Redacted.Redacted<T>): number {
return pipe(
Hash.hash(RedactedSymbolKey),
Expand All @@ -54,8 +45,20 @@ export const proto = {
export const isRedacted = (u: unknown): u is Redacted.Redacted<unknown> => hasProperty(u, RedactedTypeId)

/** @internal */
export const make = <T>(value: T): Redacted.Redacted<T> => {
const redacted = Object.create(proto)
export const make = <T>(value: T, str = "<redacted>"): Redacted.Redacted<T> => {
const redacted = Object.create({
...proto,
toString() {
return str
},
toJSON() {
return str
},
[NodeInspectSymbol]() {
return str
},
}
)
redactedRegistry.set(redacted, value)
return redacted
}
Expand Down
10 changes: 10 additions & 0 deletions packages/effect/test/Redacted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@ describe("Redacted", () => {
assert.strictEqual(`${redacted}`, "<redacted>")
})

it("toString - custom string", () => {
const redacted = Redacted.make("redacted", "API_KEY")
assert.strictEqual(`${redacted}`, "API_KEY")
})

it("toJSON", () => {
const redacted = Redacted.make("redacted")
assert.strictEqual(JSON.stringify(redacted), "\"<redacted>\"")
})

it("toJSON - custom string", () => {
const redacted = Redacted.make("redacted", "API_KEY")
assert.strictEqual(JSON.stringify(redacted), "\"API_KEY\"")
})

it("unsafeWipe", () => {
const redacted = Redacted.make("redacted")
assert.isTrue(Redacted.unsafeWipe(redacted))
Expand Down