urns
Version:
An RFC 8141 compliant URN library with some interesting type related functionality
49 lines (48 loc) • 2.55 kB
TypeScript
import { ParsedURN, FullURN, BaseURN } from "./types";
/** Used to pass q/r/f components into the urn construction */
export interface ComponentMaps {
r?: string;
q?: Record<string, string> | string;
f?: string;
}
/**
* This function takes a given NID and NSS and creates a full URN. This is relatively straight forward. The
* only real complexity comes from handling the URI encoding.
* @param nid
* @param nss
* @param {boolean=} skipVerification skip test against rfc8141 regex (optional, default false)
* @returns
*/
export declare function createFullURN<NID extends string = string, NSS extends string = string>(nid: NID, nss: NSS, components?: ComponentMaps, skipVerification?: boolean): FullURN<NID, NSS, string>;
/**
* This function takes a given NID and NSS and creates a URN. This is relatively straight forward. The
* only real complexity comes from handling the URI encoding.
* @param nid
* @param nss
* @param {boolean=} skipVerification skip test against rfc8141 regex (optional, default false)
* @returns
*/
export declare function createURN<NID extends string = string, NSS extends string = string>(nid: NID, nss: NSS, skipVerification?: boolean): BaseURN<NID, NSS>;
/**
* This function "unparses" a URN. That is to say that it takes the normal output
* of the `parse` function and reverses it to form the original URN. This is quite
* similar to the `createURN` function above except that it handles components as
* well.
* @param p
* @param {boolean=} skipVerification skip test against rfc8141 regex (optional, default false)
* @returns
*/
export declare function unparseURN<NID extends string = string, NSS extends string = string>(p: Omit<ParsedURN<NID, NSS>, "nss_encoded">, skipVerification?: boolean): FullURN<NID, NSS, string>;
/** A helper function to extract just the namespace identifier (NID) */
export declare function nid<NID extends string>(s: FullURN<NID, string, string>): NID;
/** A helper function to extract just the namespace specific string (NSS) */
export declare function nss<NSS extends string>(s: FullURN<string, NSS, string>): NSS;
/**
* This function parses a string as a URN and returns an object containing all
* the various aspects of the URN. Much of the "heavy lifting" here is done
* by the regular expression parsing itself. But there are a few more bits
* we need to do in the function as well.
* @param s
* @returns
*/
export declare function parseURN<NID extends string = string, NSS extends string = string>(s: string): ParsedURN<NID, NSS>;