@wix/sdk-types
Advanced tools
Comparing version 1.8.0 to 1.9.0
@@ -108,3 +108,303 @@ type HostModule<T, H extends Host> = { | ||
type BuildServicePluginDefinition<T extends ServicePluginDefinition<any>> = (implementation: T['__contract']) => void; | ||
declare const SERVICE_PLUGIN_ERROR_TYPE = "wix_spi_error"; | ||
export { type APIMetadata, type AuthenticationStrategy, type BaseEventMetadata, type BoundAuthenticationStrategy, type BuildEventDefinition, type BuildRESTFunction, type BuildServicePluginDefinition, EventDefinition, type EventHandler, type EventIdentity, type Host, type HostModule, type HostModuleAPI, type HttpClient, type HttpResponse, type RESTFunctionDescriptor, type RequestOptions, type RequestOptionsFactory, type ServicePluginContract, ServicePluginDefinition, type ServicePluginMethodInput, type ServicePluginMethodMetadata }; | ||
type RequestContext = { | ||
isSSR: boolean; | ||
host: string; | ||
protocol?: string; | ||
}; | ||
type ResponseTransformer = (data: any, headers?: any) => any; | ||
/** | ||
* Ambassador request options types are copied mostly from AxiosRequestConfig. | ||
* They are copied and not imported to reduce the amount of dependencies (to reduce install time). | ||
* https://github.com/axios/axios/blob/3f53eb6960f05a1f88409c4b731a40de595cb825/index.d.ts#L307-L315 | ||
*/ | ||
type Method = 'get' | 'GET' | 'delete' | 'DELETE' | 'head' | 'HEAD' | 'options' | 'OPTIONS' | 'post' | 'POST' | 'put' | 'PUT' | 'patch' | 'PATCH' | 'purge' | 'PURGE' | 'link' | 'LINK' | 'unlink' | 'UNLINK'; | ||
type AmbassadorRequestOptions<T = any> = { | ||
_?: T; | ||
url?: string; | ||
method?: Method; | ||
params?: any; | ||
data?: any; | ||
transformResponse?: ResponseTransformer | ResponseTransformer[]; | ||
}; | ||
type AmbassadorFactory<Request, Response> = (payload: Request) => ((context: RequestContext) => AmbassadorRequestOptions<Response>) & { | ||
__isAmbassador: boolean; | ||
}; | ||
type AmbassadorFunctionDescriptor<Request = any, Response = any> = AmbassadorFactory<Request, Response>; | ||
type BuildAmbassadorFunction<T extends AmbassadorFunctionDescriptor> = T extends AmbassadorFunctionDescriptor<infer Request, infer Response> ? (req: Request) => Promise<Response> : never; | ||
declare global { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- It has to be an `interface` so that it can be merged. | ||
interface SymbolConstructor { | ||
readonly observable: symbol; | ||
} | ||
} | ||
declare const emptyObjectSymbol: unique symbol; | ||
/** | ||
Represents a strictly empty plain object, the `{}` value. | ||
When you annotate something as the type `{}`, it can be anything except `null` and `undefined`. This means that you cannot use `{}` to represent an empty plain object ([read more](https://stackoverflow.com/questions/47339869/typescript-empty-object-and-any-difference/52193484#52193484)). | ||
@example | ||
``` | ||
import type {EmptyObject} from 'type-fest'; | ||
// The following illustrates the problem with `{}`. | ||
const foo1: {} = {}; // Pass | ||
const foo2: {} = []; // Pass | ||
const foo3: {} = 42; // Pass | ||
const foo4: {} = {a: 1}; // Pass | ||
// With `EmptyObject` only the first case is valid. | ||
const bar1: EmptyObject = {}; // Pass | ||
const bar2: EmptyObject = 42; // Fail | ||
const bar3: EmptyObject = []; // Fail | ||
const bar4: EmptyObject = {a: 1}; // Fail | ||
``` | ||
Unfortunately, `Record<string, never>`, `Record<keyof any, never>` and `Record<never, never>` do not work. See {@link https://github.com/sindresorhus/type-fest/issues/395 #395}. | ||
@category Object | ||
*/ | ||
type EmptyObject = {[emptyObjectSymbol]?: never}; | ||
/** | ||
Returns a boolean for whether the two given types are equal. | ||
@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650 | ||
@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796 | ||
Use-cases: | ||
- If you want to make a conditional branch based on the result of a comparison of two types. | ||
@example | ||
``` | ||
import type {IsEqual} from 'type-fest'; | ||
// This type returns a boolean for whether the given array includes the given item. | ||
// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal. | ||
type Includes<Value extends readonly any[], Item> = | ||
Value extends readonly [Value[0], ...infer rest] | ||
? IsEqual<Value[0], Item> extends true | ||
? true | ||
: Includes<rest, Item> | ||
: false; | ||
``` | ||
@category Type Guard | ||
@category Utilities | ||
*/ | ||
type IsEqual<A, B> = | ||
(<G>() => G extends A ? 1 : 2) extends | ||
(<G>() => G extends B ? 1 : 2) | ||
? true | ||
: false; | ||
/** | ||
Filter out keys from an object. | ||
Returns `never` if `Exclude` is strictly equal to `Key`. | ||
Returns `never` if `Key` extends `Exclude`. | ||
Returns `Key` otherwise. | ||
@example | ||
``` | ||
type Filtered = Filter<'foo', 'foo'>; | ||
//=> never | ||
``` | ||
@example | ||
``` | ||
type Filtered = Filter<'bar', string>; | ||
//=> never | ||
``` | ||
@example | ||
``` | ||
type Filtered = Filter<'bar', 'foo'>; | ||
//=> 'bar' | ||
``` | ||
@see {Except} | ||
*/ | ||
type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true ? never : (KeyType extends ExcludeType ? never : KeyType); | ||
type ExceptOptions = { | ||
/** | ||
Disallow assigning non-specified properties. | ||
Note that any omitted properties in the resulting type will be present in autocomplete as `undefined`. | ||
@default false | ||
*/ | ||
requireExactProps?: boolean; | ||
}; | ||
/** | ||
Create a type from an object type without certain keys. | ||
We recommend setting the `requireExactProps` option to `true`. | ||
This type is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#the-omit-helper-type). The `Omit` type does not restrict the omitted keys to be keys present on the given type, while `Except` does. The benefits of a stricter type are avoiding typos and allowing the compiler to pick up on rename refactors automatically. | ||
This type was proposed to the TypeScript team, which declined it, saying they prefer that libraries implement stricter versions of the built-in types ([microsoft/TypeScript#30825](https://github.com/microsoft/TypeScript/issues/30825#issuecomment-523668235)). | ||
@example | ||
``` | ||
import type {Except} from 'type-fest'; | ||
type Foo = { | ||
a: number; | ||
b: string; | ||
}; | ||
type FooWithoutA = Except<Foo, 'a'>; | ||
//=> {b: string} | ||
const fooWithoutA: FooWithoutA = {a: 1, b: '2'}; | ||
//=> errors: 'a' does not exist in type '{ b: string; }' | ||
type FooWithoutB = Except<Foo, 'b', {requireExactProps: true}>; | ||
//=> {a: number} & Partial<Record<"b", never>> | ||
const fooWithoutB: FooWithoutB = {a: 1, b: '2'}; | ||
//=> errors at 'b': Type 'string' is not assignable to type 'undefined'. | ||
``` | ||
@category Object | ||
*/ | ||
type Except<ObjectType, KeysType extends keyof ObjectType, Options extends ExceptOptions = {requireExactProps: false}> = { | ||
[KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType]; | ||
} & (Options['requireExactProps'] extends true | ||
? Partial<Record<KeysType, never>> | ||
: {}); | ||
/** | ||
Extract the keys from a type where the value type of the key extends the given `Condition`. | ||
Internally this is used for the `ConditionalPick` and `ConditionalExcept` types. | ||
@example | ||
``` | ||
import type {ConditionalKeys} from 'type-fest'; | ||
interface Example { | ||
a: string; | ||
b: string | number; | ||
c?: string; | ||
d: {}; | ||
} | ||
type StringKeysOnly = ConditionalKeys<Example, string>; | ||
//=> 'a' | ||
``` | ||
To support partial types, make sure your `Condition` is a union of undefined (for example, `string | undefined`) as demonstrated below. | ||
@example | ||
``` | ||
import type {ConditionalKeys} from 'type-fest'; | ||
type StringKeysAndUndefined = ConditionalKeys<Example, string | undefined>; | ||
//=> 'a' | 'c' | ||
``` | ||
@category Object | ||
*/ | ||
type ConditionalKeys<Base, Condition> = NonNullable< | ||
// Wrap in `NonNullable` to strip away the `undefined` type from the produced union. | ||
{ | ||
// Map through all the keys of the given base type. | ||
[Key in keyof Base]: | ||
// Pick only keys with types extending the given `Condition` type. | ||
Base[Key] extends Condition | ||
// Retain this key since the condition passes. | ||
? Key | ||
// Discard this key since the condition fails. | ||
: never; | ||
// Convert the produced object into a union type of the keys which passed the conditional test. | ||
}[keyof Base] | ||
>; | ||
/** | ||
Exclude keys from a shape that matches the given `Condition`. | ||
This is useful when you want to create a new type with a specific set of keys from a shape. For example, you might want to exclude all the primitive properties from a class and form a new shape containing everything but the primitive properties. | ||
@example | ||
``` | ||
import type {Primitive, ConditionalExcept} from 'type-fest'; | ||
class Awesome { | ||
name: string; | ||
successes: number; | ||
failures: bigint; | ||
run() {} | ||
} | ||
type ExceptPrimitivesFromAwesome = ConditionalExcept<Awesome, Primitive>; | ||
//=> {run: () => void} | ||
``` | ||
@example | ||
``` | ||
import type {ConditionalExcept} from 'type-fest'; | ||
interface Example { | ||
a: string; | ||
b: string | number; | ||
c: () => void; | ||
d: {}; | ||
} | ||
type NonStringKeysOnly = ConditionalExcept<Example, string>; | ||
//=> {b: string | number; c: () => void; d: {}} | ||
``` | ||
@category Object | ||
*/ | ||
type ConditionalExcept<Base, Condition> = Except< | ||
Base, | ||
ConditionalKeys<Base, Condition> | ||
>; | ||
/** | ||
* Descriptors are objects that describe the API of a module, and the module | ||
* can either be a REST module or a host module. | ||
* This type is recursive, so it can describe nested modules. | ||
*/ | ||
type Descriptors = RESTFunctionDescriptor | AmbassadorFunctionDescriptor | HostModule<any, any> | EventDefinition<any> | ServicePluginDefinition<any> | { | ||
[key: string]: Descriptors | PublicMetadata | any; | ||
}; | ||
/** | ||
* This type takes in a descriptors object of a certain Host (including an `unknown` host) | ||
* and returns an object with the same structure, but with all descriptors replaced with their API. | ||
* Any non-descriptor properties are removed from the returned object, including descriptors that | ||
* do not match the given host (as they will not work with the given host). | ||
*/ | ||
type BuildDescriptors<T extends Descriptors, H extends Host<any> | undefined, Depth extends number = 5> = { | ||
done: T; | ||
recurse: T extends { | ||
__type: typeof SERVICE_PLUGIN_ERROR_TYPE; | ||
} ? never : T extends AmbassadorFunctionDescriptor ? BuildAmbassadorFunction<T> : T extends RESTFunctionDescriptor ? BuildRESTFunction<T> : T extends EventDefinition<any> ? BuildEventDefinition<T> : T extends ServicePluginDefinition<any> ? BuildServicePluginDefinition<T> : T extends HostModule<any, any> ? HostModuleAPI<T> : ConditionalExcept<{ | ||
[Key in keyof T]: T[Key] extends Descriptors ? BuildDescriptors<T[Key], H, [ | ||
-1, | ||
0, | ||
1, | ||
2, | ||
3, | ||
4, | ||
5 | ||
][Depth]> : never; | ||
}, EmptyObject>; | ||
}[Depth extends -1 ? 'done' : 'recurse']; | ||
type PublicMetadata = { | ||
PACKAGE_NAME?: string; | ||
}; | ||
export { type APIMetadata, type AmbassadorFactory, type AmbassadorFunctionDescriptor, type AmbassadorRequestOptions, type AuthenticationStrategy, type BaseEventMetadata, type BoundAuthenticationStrategy, type BuildAmbassadorFunction, type BuildDescriptors, type BuildEventDefinition, type BuildRESTFunction, type BuildServicePluginDefinition, type Descriptors, EventDefinition, type EventHandler, type EventIdentity, type Host, type HostModule, type HostModuleAPI, type HttpClient, type HttpResponse, type Method, type PublicMetadata, type RESTFunctionDescriptor, type RequestContext, type RequestOptions, type RequestOptionsFactory, SERVICE_PLUGIN_ERROR_TYPE, type ServicePluginContract, ServicePluginDefinition, type ServicePluginMethodInput, type ServicePluginMethodMetadata }; |
@@ -24,2 +24,3 @@ "use strict"; | ||
EventDefinition: () => EventDefinition, | ||
SERVICE_PLUGIN_ERROR_TYPE: () => SERVICE_PLUGIN_ERROR_TYPE, | ||
ServicePluginDefinition: () => ServicePluginDefinition | ||
@@ -47,6 +48,8 @@ }); | ||
} | ||
var SERVICE_PLUGIN_ERROR_TYPE = "wix_spi_error"; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
EventDefinition, | ||
SERVICE_PLUGIN_ERROR_TYPE, | ||
ServicePluginDefinition | ||
}); |
{ | ||
"name": "@wix/sdk-types", | ||
"version": "1.8.0", | ||
"version": "1.9.0", | ||
"license": "UNLICENSED", | ||
@@ -35,2 +35,3 @@ "author": { | ||
"tsup": "^7.3.0", | ||
"type-fest": "^4.9.0", | ||
"typescript": "^5.3.3" | ||
@@ -61,3 +62,3 @@ }, | ||
}, | ||
"falconPackageHash": "48f0d7162f08bb7b5de9f622d6232edd13533a09d57216d579952474" | ||
"falconPackageHash": "2fb625b6ef9fd24f133e43d99abeee52ad19f9e49b2f2fd6d48d8aa9" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33275
448
6