Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramon Gebben committed Dec 20, 2024
0 parents commit bb24e9b
Show file tree
Hide file tree
Showing 36 changed files with 7,872 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'

- name: Install dependencies
run: npm install

- name: Run TypeScript build
run: npm run build

- name: Run tests
run: npm test

- name: Upload coverage report
if: success()
uses: actions/upload-artifact@v3
with:
name: coverage-report
path: coverage/
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
src/
node_modules/
*.log
*.ts
package-lock.json
8 changes: 8 additions & 0 deletions .prettierrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
printWidth: 80
semi: true
singleQuote: true
trailingComma: 'all'
tabWidth: 2
useTabs: false
bracketSpacing: true
arrowParens: 'avoid'
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 DatHuis

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
166 changes: 166 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# @bndl-io/context-providers-provider

A React utility component for managing complex context dependencies. Automatically sorts and nests context providers based on dependencies, promoting clean and maintainable patterns for context usage.

## Why?

React applications often rely on multiple context providers to manage state, theming, authentication, and more. However, managing these providers can quickly lead to deeply nested and unmanageable component trees, especially when providers depend on one another.

### Common Problems:

1. **Context Hell**: Wrapping components in multiple layers of context providers makes the tree hard to read and maintain.
2. **Dependency Issues**: Incorrect nesting of providers can lead to runtime errors or unexpected behavior.
3. **Encapsulation**: Logic for initializing context values often leaks outside of dedicated components, making the code harder to reuse or test.

### How `@bndl-io/context-providers-provider` Solves This:

- Automatically **sorts providers** based on their dependencies.
- Detects **circular dependencies** and throws helpful error messages.
- Encourages encapsulation by requiring all context logic to reside in dedicated `ProviderComponents`.
- Simplifies application structure by managing provider nesting for you.

---

## Installation

Install the library via npm:

```bash
npm install @bndl-io/context-providers-provider
```

or with yarn:

```bash
yarn add @bndl-io/context-providers-provider
```

---

## Usage

### Define Your Providers

Each provider is defined as an object with the following structure:

```ts
type ProviderConfig = {
name: string;
C: (children: ReactNode) => JSX.Element; // The provider component
dependencies: string[]; // Names of other providers this provider depends on
};
```

For example:

```tsx
const providers = [
{
name: 'Auth',
C: children => <AuthProvider>{children}</AuthProvider>,
dependencies: ['Theme'], // Auth depends on Theme
},
{
name: 'Theme',
C: children => <ThemeProvider>{children}</ThemeProvider>,
dependencies: [], // No dependencies
},
{
name: 'Config',
C: children => <ConfigProvider>{children}</ConfigProvider>,
dependencies: ['Auth'], // Config depends on Auth
},
];
```

---

### Wrap Your Application

Use the `ContextProvidersProvider` to automatically sort and nest providers based on their dependencies:

```tsx
import React from 'react';
import ContextProvidersProvider from '@bndl-io/context-providers-provider';

const providers = [
{
name: 'Auth',
C: children => <AuthProvider>{children}</AuthProvider>,
dependencies: ['Theme'],
},
{
name: 'Theme',
C: children => <ThemeProvider>{children}</ThemeProvider>,
dependencies: [],
},
{
name: 'Config',
C: children => <ConfigProvider>{children}</ConfigProvider>,
dependencies: ['Auth'],
},
];

const App = () => {
return (
<ContextProvidersProvider providers={providers}>
<YourApp />
</ContextProvidersProvider>
);
};
```

---

### Example Output

The above setup would result in the following provider tree:

```tsx
<ThemeProvider>
<AuthProvider>
<ConfigProvider>
<YourApp />
</ConfigProvider>
</AuthProvider>
</ThemeProvider>
```

---

## API

### `ContextProvidersProvider`

#### Props:

- `providers`: An array of `ProviderConfig` objects. Each config must include:
- `name`: A unique string identifying the provider.
- `C`: A function that renders the provider component, wrapping its children.
- `dependencies`: An array of strings identifying the providers this provider depends on.
- `children`: The children to be rendered inside the nested providers.

#### Errors:

- Throws an error if a provider has dependencies that don’t exist.
- Throws an error if circular dependencies are detected.

---

## License

MIT License

Copyright (c) DatHuis B.V.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

---

## Contributing

We welcome contributions! Please open an issue or submit a pull request for any bugs, features, or enhancements.
77 changes: 77 additions & 0 deletions coverage/clover.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1734687839701" clover="3.2.0">
<project timestamp="1734687839701" name="All files">
<metrics statements="53" coveredstatements="53" conditionals="11" coveredconditionals="11" methods="10" coveredmethods="10" elements="74" coveredelements="74" complexity="0" loc="53" ncloc="53" packages="3" files="3" classes="3"/>
<package name="ContextProvidersProvider">
<metrics statements="14" coveredstatements="14" conditionals="1" coveredconditionals="1" methods="2" coveredmethods="2"/>
<file name="index.tsx" path="/Users/ramon/Projects/ContextProvidersProvider/src/ContextProvidersProvider/index.tsx">
<metrics statements="14" coveredstatements="14" conditionals="1" coveredconditionals="1" methods="2" coveredmethods="2"/>
<line num="1" count="1" type="stmt"/>
<line num="2" count="1" type="stmt"/>
<line num="3" count="1" type="stmt"/>
<line num="30" count="1" type="stmt"/>
<line num="31" count="13" type="stmt"/>
<line num="32" count="12" type="stmt"/>
<line num="34" count="12" type="stmt"/>
<line num="35" count="8" type="stmt"/>
<line num="43" count="4" type="stmt"/>
<line num="44" count="13" type="cond" truecount="1" falsecount="0"/>
<line num="46" count="9" type="stmt"/>
<line num="47" count="9" type="stmt"/>
<line num="50" count="4" type="stmt"/>
<line num="53" count="1" type="stmt"/>
</file>
</package>
<package name="ContextProvidersProvider.utils.detectCircularDependencies">
<metrics statements="16" coveredstatements="16" conditionals="6" coveredconditionals="6" methods="3" coveredmethods="3"/>
<file name="index.ts" path="/Users/ramon/Projects/ContextProvidersProvider/src/ContextProvidersProvider/utils/detectCircularDependencies/index.ts">
<metrics statements="16" coveredstatements="16" conditionals="6" coveredconditionals="6" methods="3" coveredmethods="3"/>
<line num="9" count="2" type="stmt"/>
<line num="10" count="18" type="stmt"/>
<line num="12" count="39" type="stmt"/>
<line num="13" count="39" type="stmt"/>
<line num="16" count="18" type="stmt"/>
<line num="17" count="18" type="stmt"/>
<line num="25" count="18" type="stmt"/>
<line num="26" count="58" type="cond" truecount="1" falsecount="0"/>
<line num="27" count="7" type="stmt"/>
<line num="29" count="51" type="cond" truecount="1" falsecount="0"/>
<line num="31" count="43" type="stmt"/>
<line num="32" count="43" type="cond" truecount="4" falsecount="0"/>
<line num="33" count="27" type="stmt"/>
<line num="34" count="27" type="stmt"/>
<line num="37" count="18" type="stmt"/>
<line num="40" count="2" type="stmt"/>
</file>
</package>
<package name="ContextProvidersProvider.utils.sortProviders">
<metrics statements="23" coveredstatements="23" conditionals="4" coveredconditionals="4" methods="5" coveredmethods="5"/>
<file name="index.ts" path="/Users/ramon/Projects/ContextProvidersProvider/src/ContextProvidersProvider/utils/sortProviders/index.ts">
<metrics statements="23" coveredstatements="23" conditionals="4" coveredconditionals="4" methods="5" coveredmethods="5"/>
<line num="10" count="2" type="stmt"/>
<line num="11" count="14" type="stmt"/>
<line num="12" count="14" type="stmt"/>
<line num="13" count="14" type="stmt"/>
<line num="15" count="14" type="stmt"/>
<line num="16" count="33" type="cond" truecount="1" falsecount="0"/>
<line num="17" count="1" type="stmt"/>
<line num="21" count="32" type="cond" truecount="1" falsecount="0"/>
<line num="22" count="1" type="stmt"/>
<line num="25" count="31" type="stmt"/>
<line num="27" count="31" type="stmt"/>
<line num="28" count="40" type="stmt"/>
<line num="29" count="19" type="cond" truecount="1" falsecount="0"/>
<line num="30" count="5" type="stmt"/>
<line num="34" count="14" type="stmt"/>
<line num="37" count="22" type="stmt"/>
<line num="38" count="22" type="stmt"/>
<line num="39" count="22" type="stmt"/>
<line num="42" count="14" type="stmt"/>
<line num="43" count="28" type="cond" truecount="1" falsecount="0"/>
<line num="44" count="19" type="stmt"/>
<line num="48" count="8" type="stmt"/>
<line num="51" count="2" type="stmt"/>
</file>
</package>
</project>
</coverage>
Loading

0 comments on commit bb24e9b

Please sign in to comment.