Skip to content

Commit

Permalink
Allow HTTP options (#468)
Browse files Browse the repository at this point in the history
Allow setting `fetchOptions` to `useAudiences` and `AudienceEditor`, so
client apps can use a remote audiences service with proper
authentication.
  • Loading branch information
xjunior authored Dec 19, 2024
1 parent 8d23a51 commit b5a7f72
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
4 changes: 4 additions & 0 deletions audiences-react/docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

# Version 1.4.0 (2024-12-18)

- Allow setting fetch options to all API [#468](https://github.com/powerhome/audiences/pull/468)

# Version 1.3.0 (2024-12-12)

- Protect SCIM search from backend [#451](https://github.com/powerhome/audiences/pull/451)
Expand Down
10 changes: 10 additions & 0 deletions audiences-react/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ With everything in place, the usage should look like this:
<AudienceEditor uri={audienceContextUri} scimUri={scimV2Uri} />
```

You can also add arguments to the fetch calls, like headers:

```jsx
<AudienceEditor
uri={audienceContextUri}
scimUri={scimV2Uri}
fetchOptions={{ headers: { Authorization: "Bearer my-token" } }}
/>
```

See [example](../src/example.tsx).

### Peer Dependencies
Expand Down
2 changes: 1 addition & 1 deletion audiences-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "audiences",
"version": "1.3.0",
"version": "1.4.0",
"description": "Audiences SCIM client",
"files": [
"dist/*.*",
Expand Down
4 changes: 3 additions & 1 deletion audiences-react/src/AudienceForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ type AudienceFormProps = {
userResource: string
groupResources: string[]
allowIndividuals: boolean
fetchOptions: Parameters<typeof useAudiences>[1]
}

export const AudienceForm = ({
uri,
userResource,
groupResources,
allowIndividuals = true,
fetchOptions,
}: AudienceFormProps) => {
const [editing, setEditing] = useState<number>()

Expand All @@ -36,7 +38,7 @@ export const AudienceForm = ({
reset,
removeCriteria,
updateCriteria,
} = useAudiences(uri)
} = useAudiences(uri, fetchOptions)

const handleRemoveCriteria = (index: number) => {
if (confirm("Remove criteria?")) {
Expand Down
13 changes: 8 additions & 5 deletions audiences-react/src/audiences.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect } from "react"
import useFetch, { CachePolicies } from "use-http"
import useFetch, { CachePolicies, IncomingOptions } from "use-http"

import useFormReducer, {
RegistryAction,
Expand All @@ -16,7 +16,7 @@ type UpdateCriteriaAction = RegistryAction & {
criterion: GroupCriterion
}

type UseAudienceContext = UseFormReducer<AudienceContext> & {
export type UseAudienceContext = UseFormReducer<AudienceContext> & {
saving: boolean
save: () => void
fetchUsers: (
Expand All @@ -28,14 +28,17 @@ type UseAudienceContext = UseFormReducer<AudienceContext> & {
updateCriteria: (index: number, criteria: GroupCriterion) => void
}

export function useAudiences(uri: string): UseAudienceContext {
const { data } = useFetch(uri, [uri])
export function useAudiences(
uri: string,
options: IncomingOptions = {},
): UseAudienceContext {
const { data } = useFetch(uri, options, [uri])
const {
get,
put,
response,
loading: saving,
} = useFetch(uri, { cachePolicy: CachePolicies.NO_CACHE })
} = useFetch(uri, { ...options, cachePolicy: CachePolicies.NO_CACHE })
const criteriaForm = useFormReducer<AudienceContext>(data, {
"remove-criteria": (
context,
Expand Down
4 changes: 4 additions & 0 deletions audiences-react/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AudienceForm } from "./AudienceForm"
import Scim from "./scim"
import { useAudiences } from "./audiences"

const UserResourceId = "Users"
const AllowedGroupIds = ["Territories", "Departments", "Titles"]
Expand All @@ -8,11 +9,13 @@ type AudienceEditorProps = {
uri: string
scimUri: string
allowIndividuals?: boolean
fetchOptions?: Parameters<typeof useAudiences>[1]
}
export function AudienceEditor({
uri,
scimUri,
allowIndividuals = true,
fetchOptions = {},
}: AudienceEditorProps) {
return (
<Scim.Provider value={scimUri}>
Expand All @@ -21,6 +24,7 @@ export function AudienceEditor({
userResource={UserResourceId}
groupResources={AllowedGroupIds}
allowIndividuals={allowIndividuals}
fetchOptions={fetchOptions}
/>
</Scim.Provider>
)
Expand Down

0 comments on commit b5a7f72

Please sign in to comment.