# fetchResource

## Content

# Function: fetchResource()

```ts
function fetchResource(resource, options?): Promise<ArrayBuffer>;
```

Fetches an external resource using a configurable resolution and loading pipeline.

This function orchestrates the full lifecycle of a resource request:
1. Resolves the final URL using [ExternalResourcesOptions.resolveUrl](../interfaces/ExternalResourcesOptions#resolveurl) (if provided)
2. Delegates the network request to a [ResourceLoader](../interfaces/ResourceLoader)
3. Invokes lifecycle hooks before and after the request
4. Handles errors and optionally reports them via a callback

## Parameters

### resource

[`ResourceProperties`](../interfaces/ResourceProperties)

The initial resource descriptor containing the resource type
and original URL (e.g., extracted from a certificate or document).

### options?

[`ExternalResourcesOptions`](../interfaces/ExternalResourcesOptions)

Optional configuration controlling URL resolution, transport,
and lifecycle hooks:
- `resolveUrl` allows rewriting or proxying the resource URL
- `loader` overrides the default network implementation
- `beforeRequest`, `afterResponse`, and `onError` provide lifecycle hooks

## Returns

`Promise`&lt;`ArrayBuffer`&gt;

A promise resolving to the fetched resource as an ArrayBuffer,
or `null` if the request fails.

## Remarks

- If `resolveUrl` is provided, it may return a modified URL (e.g., a proxy endpoint).
- If no custom loader is specified, a default Fetch API–based loader is used.
- Errors thrown during fetch are caught internally; to observe them, use `onError`.

## Example

```ts
const data = await fetchResource(
  { type: ResourceType.CRL, url: crlUrl },
  {
    resolveUrl: (ctx) => `/api/proxy?url=${encodeURIComponent(ctx.url)}`,
    onError: (ctx, err) => console.error("Failed:", ctx.url, err)
  }
);
```
