-
-
Notifications
You must be signed in to change notification settings - Fork 934
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
Open Faker for custom/community modules #704
Comments
Here is a workaround to do this right now in a typesafe way: import type { Faker, UsableLocale } from "@faker-js/faker";
import { faker } from "@faker-js/faker";
function withCustomModule<
S extends Faker,
Name extends string,
T,
U extends S & Record<Name, T>
>(base: S, name: Name, value: T): U {
return {
// Class level functions
seed: (seed: number | number[]): void => base.seed(seed),
setLocale: (locale: UsableLocale): void => base.setLocale(locale),
// Previous modules
...base,
// Custom Modules
[name]: value,
} as U;
}
const customModule = {
abc: () => "sjkdfhajkshdf",
} as const;
const fakerPlus = withCustomModule(faker, "test", customModule);
console.log(fakerPlus.test.abc()); |
Isn't that almost like my example above? |
Yes, I just wanted to provide an example of how other communities handle the extendability approach. |
If you are interested in this feature, please upvote it. |
Thank you for your feature proposal. We marked it as "waiting for user interest" for now to gather some feedback from our community:
We would also like to hear about other community members' use cases for the feature to give us a better understanding of their potential implicit or explicit requirements. We will start the implementation based on:
We do this because:
|
We might need something like this for fake with the standalone module functions. I think it might end up in something like a registry: registry.module.method |
I appreciate this issue has a lot of upvotes, but I feel I'm maybe missing something about the potential use-cases. For example, suppose you really want faker to support random tree names but you notice #2022 doesn't have enough up-votes yet. You can already easily create your own function using your own data with faker helper methods e.g. What is gained by making this one-line function into a "community module"? By using faker.helpers functions you already can take advantage of reproducible seeds etc. And core modules wouldn't be able to refer to community modules in their definitions. If you just want to share the data, you could already publish an NPM package which is just |
AFAICT: The are currently 3 things this is intended for:
But also as a reminder, that whatever we are going to do for #2667 + #2664 it should be possible for our community to do the same for their functions/modules.
What I hope to do with that:
|
But what is the advantage of having it callable via |
None, other than consistency with the other calls. It has been requested in #444.
True, and we have to look into that. Would you consider that an important distinction and why (not)?
The extension would win over the provided methods and thus nothing changes for the user, except that they might get additional suggestions for the native methods. In the end, it is just an outdated/obsolete dependency in your project, that you didn't remove because you didn't read the changelog/release announcement. As a side note:
1 is already possible, although only with some JS/TS tricks, that I don't want to endorse at this point. Speculation: Maybe we replace faker.helpers.fake with localized function invocations ( |
Clear and concise description of the problem
Faker has many different modules but even more requested features.
Many of these features are sadly too complex to be maintained by the Faker-JS team.
Suggested solution
The Faker class should accept a list of modules that will be used under the created Faker instance. These modules will be plugins maintained by the community.
Like:
Alternative
No response
Additional context
Somewhat related to #444
The text was updated successfully, but these errors were encountered: