-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fetch: switch to simplet undici fetcher
- Loading branch information
Showing
10 changed files
with
130 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { Cache } from './types' | ||
import { request } from 'undici' | ||
import { getCacheKey } from './getCacheKey' | ||
import { OpenPolicyAgentClientProps } from './types/openPolicyAgentClientProps' | ||
|
||
export class OpenPolicyAgentClient<TCache extends Cache> { | ||
public readonly cache: TCache | undefined | ||
private readonly url: string | ||
|
||
constructor( | ||
config: OpenPolicyAgentClientProps<TCache> | string, | ||
private readonly opaVersion = 'v1', | ||
private readonly method: 'POST' | 'GET' = 'POST' | ||
) { | ||
/* istanbul ignore next */ | ||
if (typeof config === 'object') { | ||
this.url = config.url | ||
|
||
if (config.opaVersion) { | ||
this.opaVersion = config.opaVersion | ||
} | ||
|
||
if (config.method) { | ||
this.method = config.method | ||
} | ||
|
||
if (config.cache) { | ||
this.cache = config.cache | ||
} | ||
} else { | ||
this.url = config | ||
} | ||
} | ||
|
||
/** | ||
* Query the requested OPA resource. | ||
* | ||
* @param resource Can be expressed both in dot notation or slash notation. | ||
* @param input OPA Query input. | ||
*/ | ||
public async query<TResponse = { result: boolean }>( | ||
resource: string, | ||
input?: unknown | ||
): Promise<TResponse> { | ||
const resourcePath = resource.replace(/\./gi, '/') | ||
|
||
const cacheKey = getCacheKey(resourcePath, input) | ||
const cached = this.cache?.get(cacheKey) as TResponse | undefined | ||
|
||
if (cached) { | ||
return cached | ||
} | ||
|
||
const res = await request( | ||
`${this.url}/${this.opaVersion}/data/${resourcePath}`, | ||
{ | ||
method: this.method, | ||
body: input ? JSON.stringify({ input }) : undefined, | ||
} | ||
) | ||
|
||
const body = (await res.body.json()) as TResponse | ||
|
||
if (this.cache) { | ||
this.cache.set(cacheKey, body) | ||
} | ||
|
||
return body | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { sha1 } from 'object-hash' | ||
|
||
export const getCacheKey = (resource: string, input?: unknown) => { | ||
return sha1({ | ||
resource, | ||
input, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type Cache = { | ||
get(key: string): any | ||
set(key: string, value: any): void | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './cache' | ||
export * from './pluginProps' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { Cache } from './cache' | ||
|
||
export type OpenPolicyAgentClientProps<TCache extends Cache> = { | ||
url: string | ||
opaVersion?: string | ||
method?: 'POST' | 'GET' | ||
cache?: TCache | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters