Skip to content

Commit

Permalink
feat(types): Added the NameValuePair and UserBase base utility types
Browse files Browse the repository at this point in the history
  • Loading branch information
sullivanpj committed Dec 12, 2024
1 parent 540743f commit ed101af
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 20 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default getStormConfig({
"react/jsx-closing-bracket-location": 0,
"no-undef": 0,
"no-unused-vars": "warn",
"no-redeclare": 0,
"unicorn/consistent-function-scoping": 0,
"class-methods-use-this": 0,
"operator-linebreak": 0,
Expand Down
12 changes: 6 additions & 6 deletions packages/serialization/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export type StormURL = ParsedURL & {
*/
export interface CookieSerializeOptions {
/**
* Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.3|Domain Set-Cookie attribute}. By default, no
* Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.3 | Domain Set-Cookie attribute}. By default, no
* domain is set, and most clients will consider the cookie to apply to only
* the current domain.
*/
Expand All @@ -208,18 +208,18 @@ export interface CookieSerializeOptions {
encode?(value: string): string;

/**
* Specifies the `Date` object to be the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.1|`Expires` `Set-Cookie` attribute}. By default,
* Specifies the `Date` object to be the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.1 | `Expires` `Set-Cookie` attribute}. By default,
* no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete
* it on a condition like exiting a web browser application.
*
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3 | cookie storage model specification}
* states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
* possible not all clients by obey this, so if both are set, they should
* point to the same date and time.
*/
expires?: Date | undefined;
/**
* Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.6|`HttpOnly` `Set-Cookie` attribute}.
* Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.6 | `HttpOnly` `Set-Cookie` attribute}.
* When truthy, the `HttpOnly` attribute is set, otherwise it is not. By
* default, the `HttpOnly` attribute is not set.
*
Expand All @@ -232,7 +232,7 @@ export interface CookieSerializeOptions {
* `Set-Cookie` attribute. The given number will be converted to an integer
* by rounding down. By default, no maximum age is set.
*
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3 | cookie storage model specification}
* states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
* possible not all clients by obey this, so if both are set, they should
* point to the same date and time.
Expand Down Expand Up @@ -276,7 +276,7 @@ export interface CookieSerializeOptions {
*/
sameSite?: true | false | "lax" | "strict" | "none" | undefined;
/**
* Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.5|`Secure` `Set-Cookie` attribute}. When truthy, the
* Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.5 | `Secure` `Set-Cookie` attribute}. When truthy, the
* `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set.
*
* *Note* be careful when setting this to `true`, as compliant clients will
Expand Down
14 changes: 3 additions & 11 deletions packages/types/src/utility-types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
-------------------------------------------------------------------*/

import { NameValuePair } from "./utilities";

export type SelectOptionValue = string | number | boolean | null;

/**
Expand All @@ -23,22 +25,12 @@ export type SelectOptionValue = string | number | boolean | null;
export interface SelectOption<
TValue extends SelectOptionValue = SelectOptionValue,
TName = string
> {
> extends NameValuePair<TValue, TName> {
/**
* The index of the select option
*/
index: number;

/**
* The string value to display in the field
*/
name: TName;

/**
* The value stored behind the scenes when selected
*/
value: TValue;

/**
* The description of the select option
*/
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/utility-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ export * from "./object";
export * from "./package-json";
export * from "./string";
export * from "./tsconfig";
export * from "./user";
export * from "./utilities";
export * from "./validations";
53 changes: 53 additions & 0 deletions packages/types/src/utility-types/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*-------------------------------------------------------------------
⚡ Storm Software - Storm Stack
This code was released as part of the Storm Stack project. Storm Stack
is maintained by Storm Software under the Apache-2.0 License, and is
free for commercial and private use. For more information, please visit
our licensing page.
Website: https://stormsoftware.com
Repository: https://github.com/storm-software/storm-stack
Documentation: https://stormsoftware.com/projects/storm-stack/docs
Contact: https://stormsoftware.com/contact
License: https://stormsoftware.com/projects/storm-stack/license
-------------------------------------------------------------------*/

export type UserType = "internal" | "external" | "service";

export const UserType = {
INTERNAL: "internal" as UserType,
EXTERNAL: "external" as UserType,
SERVICE: "service" as UserType
};

export interface UserBase {
/**
* The user's ID.
*/
id: string;

/**
* The user's full name.
*/
username?: string;

/**
* The user's type.
*
* @defaultValue "external"
*/
type: UserType;

/**
* The user's email address.
*/
email?: string;

/**
* The user's role.
*/
role?: string;
}
17 changes: 15 additions & 2 deletions packages/types/src/utility-types/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type NarrowRaw<A> =
| (A extends [] ? [] : never)
| (A extends Narrowable ? A : never)
| {
// eslint-disable-next-line @typescript-eslint/ban-types
[K in keyof A]: A[K] extends Function ? A[K] : NarrowRaw<A[K]>;
};

Expand All @@ -38,7 +37,6 @@ export type Narrow<A> = Try<A, [], NarrowRaw<A>>;
export type Try<A1, A2, Catch = never> = A1 extends A2 ? A1 : Catch;

// Hack to get TypeScript to show simplified types in error messages
// eslint-disable-next-line @typescript-eslint/ban-types
export type Pretty<T> = { [K in keyof T]: T[K] } & {};

export type ComputeRange<
Expand All @@ -49,3 +47,18 @@ export type ComputeRange<
: ComputeRange<N, [...Result, Result["length"]]>;

export type Index40 = ComputeRange<40>[number];

/**
* A utility type for specifying a name/value pair.
*/
export interface NameValuePair<TValue, TName = string> {
/**
* The name of the pair
*/
name: TName;

/**
* The value of the pair
*/
value: TValue;
}
2 changes: 1 addition & 1 deletion packages/types/src/utility-types/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type ValidationDetails<
| typeof MessageType.SUCCESS
> = MessageDetails<TMessageType> & {
/**
* The field that the message is related to.
* The field path that the message is related to.
*
* @remarks
* If `undefined` or `null`, the message is not related to a specific field - in this case it is likely a global/form message.
Expand Down

0 comments on commit ed101af

Please sign in to comment.