The encode
method is also exposed as the default export, for convenience.
The other API methods, including encode
, decode
, ifMatch
, and ifNoMatch
,
are defined as properties of the default export. Meaning it can be used as a
function and also an object:
import etag from "https://deno.land/x/etag@0.1.1/mod.ts";
etag(entity, options); // encode a tag for entity
etag.encode(entity); // same as etag(entity)
// ... see below for details on other API methods
See what we did there? We only imported
etag
, and it makes sense to see it used functionally.... but we're also using it as we would in a namespace (import * as etag
). 🧐
etag.encode(entity: string | ArrayBuffer | Uint8Array | FileInfo);
etag.decode(etag: string);
etag.ifMatch();
Note: Also exposed as etag.match()
etag.ifNoMatch();
Note: also accessible as
etag.ifNoneMatch
Here's a basic example of returning a Response body with a weak eTag attached:
return new Response(body, {
headers: {
"etag": etag(body),
// ≈ etag.encode(body)
},
});
const svg = new Blob(['<svg xmlns="http://www.w3.org/2000/svg" />']);
etag(svg, false);
// ≈ etag.encode(svg, { weak: false })
The factory
function exported in mod.ts
is a quick and easy way to add ETag
support to a project running on the Oak server framework. It probably supports
other frameworks as well, but I haven't had the chance to investigate that yet.
app.use("/:img.png", etag.factory);
How's it work? The factory function returns a closure which receives the application context object, giving it the ability to intercept and modify headers on every request/response.
Register the factory as a middleware with your framework, define a specific URL pattern for it to be triggered on, and a deterministic ETag will be generated and injected into the response headers for all requests that match its registered pattern.
Unless, of course, you're fixing a bug for which an issue already exists!
This allows the issue to be connected to your Pull Request, creating a permanent record of your contribution to the project. It also makes it easier for maintainers to track project progression.
This section assumes you have the GitHub CLI. You definitely should have it.
gh repo fork deno911/etag --clone
git checkout -b fix/typo-in-readme
# hack hack hack...
git commit README.md -m "fix: typos in README.md" && git push
Note: keep the scope of your changes relevant and concise.
gh pr create --title "fix: typos in README.md"
Or just open your repo on github.com and follow the prompts.
Warning: make sure you select the upstream repo for your PR!