@wix/sdk-runtime
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -0,2 +1,68 @@ | ||
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; | ||
} | ||
} | ||
/** | ||
Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability. | ||
@example | ||
``` | ||
import type {Simplify} from 'type-fest'; | ||
type PositionProps = { | ||
top: number; | ||
left: number; | ||
}; | ||
type SizeProps = { | ||
width: number; | ||
height: number; | ||
}; | ||
// In your editor, hovering over `Props` will show a flattened object with all the properties. | ||
type Props = Simplify<PositionProps & SizeProps>; | ||
``` | ||
Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface. | ||
If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`. | ||
@example | ||
``` | ||
import type {Simplify} from 'type-fest'; | ||
interface SomeInterface { | ||
foo: number; | ||
bar?: string; | ||
baz: number | undefined; | ||
} | ||
type SomeType = { | ||
foo: number; | ||
bar?: string; | ||
baz: number | undefined; | ||
}; | ||
const literal = {foo: 123, bar: 'hello', baz: 456}; | ||
const someType: SomeType = literal; | ||
const someInterface: SomeInterface = literal; | ||
function fn(object: Record<string, unknown>): void {} | ||
fn(literal); // Good: literal object type is sealed | ||
fn(someType); // Good: type is sealed | ||
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened | ||
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type` | ||
``` | ||
@link https://github.com/microsoft/TypeScript/issues/15300 | ||
@category Object | ||
*/ | ||
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {}; | ||
/** | ||
* Recursively rename nested keys provided in `renameMap` in the given object. | ||
@@ -11,5 +77,50 @@ * Providing a list of paths to ignore will prevent renaming of keys in nested objects. | ||
*/ | ||
declare function renameAllNestedKeys(payload: Record<string, unknown> | null | undefined, renameMap: Record<string, string>, ignorePaths: string[]): Record<string, unknown> | null | undefined; | ||
declare function renameKeysFromSDKRequestToRESTRequest<T>(payload: T, ignorePaths?: string[]): T; | ||
declare function renameKeysFromRESTResponseToSDKResponse<T>(payload: T, ignorePaths?: string[]): T; | ||
declare function renameAllNestedKeys<T, KeyMapper extends Record<string, string>>(payload: T, renameMap: KeyMapper, ignorePaths: string[]): RenameAllNestedKeys<T, KeyMapper>; | ||
declare function renameKeysFromSDKRequestToRESTRequest<T>(payload: T, ignorePaths?: string[]): MapKeys<{ [P in keyof T]: TransformedValue<T[P], { | ||
readonly _id: "id"; | ||
readonly _createdDate: "createdDate"; | ||
readonly _updatedDate: "updatedDate"; | ||
}>; }, { | ||
readonly _id: "id"; | ||
readonly _createdDate: "createdDate"; | ||
readonly _updatedDate: "updatedDate"; | ||
}> extends infer T_1 ? { [KeyType_1 in keyof T_1]: MapKeys<{ [P in keyof T]: TransformedValue<T[P], { | ||
readonly _id: "id"; | ||
readonly _createdDate: "createdDate"; | ||
readonly _updatedDate: "updatedDate"; | ||
}>; }, { | ||
readonly _id: "id"; | ||
readonly _createdDate: "createdDate"; | ||
readonly _updatedDate: "updatedDate"; | ||
}>[KeyType_1]; } : never; | ||
declare function renameKeysFromRESTResponseToSDKResponse<T>(payload: T, ignorePaths?: string[]): MapKeys<{ [P in keyof T]: TransformedValue<T[P], { | ||
readonly id: "_id"; | ||
readonly createdDate: "_createdDate"; | ||
readonly updatedDate: "_updatedDate"; | ||
}>; }, { | ||
readonly id: "_id"; | ||
readonly createdDate: "_createdDate"; | ||
readonly updatedDate: "_updatedDate"; | ||
}> extends infer T_1 ? { [KeyType_1 in keyof T_1]: MapKeys<{ [P in keyof T]: TransformedValue<T[P], { | ||
readonly id: "_id"; | ||
readonly createdDate: "_createdDate"; | ||
readonly updatedDate: "_updatedDate"; | ||
}>; }, { | ||
readonly id: "_id"; | ||
readonly createdDate: "_createdDate"; | ||
readonly updatedDate: "_updatedDate"; | ||
}>[KeyType_1]; } : never; | ||
type RenameAllNestedKeys<T, KeyMapper extends Record<string, string>> = Simplify<MapKeys<{ | ||
[P in keyof T]: TransformedValue<T[P], KeyMapper>; | ||
}, KeyMapper>>; | ||
type TransformedValue<T, KeyMapper extends Record<string, string>> = T extends Date ? T : T extends RegExp ? T : T extends (infer E)[] ? TransformedArray<E, KeyMapper> : T extends object ? RenameAllNestedKeys<T, KeyMapper> : T; | ||
interface TransformedArray<T, KeyMapper extends Record<string, string>> extends Array<TransformedValue<T, KeyMapper>> { | ||
} | ||
type MapKeys<T, M extends Record<string, string>> = KeyValueTupleToObject<ValueOf<{ | ||
[K in keyof T]-?: [K extends keyof M ? M[K] : K, T[K]]; | ||
}>>; | ||
type KeyValueTupleToObject<T extends [keyof any, any]> = { | ||
[K in T[0]]: Extract<T, [K, any]>[1]; | ||
}; | ||
type ValueOf<T> = T[keyof T]; | ||
@@ -51,2 +162,2 @@ type ValidationErrorResponse = { | ||
export { type ApplicationErrorResponse, type HTTPClientError, type ValidationErrorResponse, renameAllNestedKeys, renameKeysFromRESTResponseToSDKResponse, renameKeysFromSDKRequestToRESTRequest, transformError }; | ||
export { type ApplicationErrorResponse, type HTTPClientError, type RenameAllNestedKeys, type ValidationErrorResponse, renameAllNestedKeys, renameKeysFromRESTResponseToSDKResponse, renameKeysFromSDKRequestToRESTRequest, transformError }; |
@@ -57,22 +57,16 @@ "use strict"; | ||
function renameKeysFromSDKRequestToRESTRequest(payload, ignorePaths = []) { | ||
return renameAllNestedKeys( | ||
payload, | ||
{ | ||
_id: "id", | ||
_createdDate: "createdDate", | ||
_updatedDate: "updatedDate" | ||
}, | ||
ignorePaths | ||
); | ||
const renameMap = { | ||
_id: "id", | ||
_createdDate: "createdDate", | ||
_updatedDate: "updatedDate" | ||
}; | ||
return renameAllNestedKeys(payload, renameMap, ignorePaths); | ||
} | ||
function renameKeysFromRESTResponseToSDKResponse(payload, ignorePaths = []) { | ||
return renameAllNestedKeys( | ||
payload, | ||
{ | ||
id: "_id", | ||
createdDate: "_createdDate", | ||
updatedDate: "_updatedDate" | ||
}, | ||
ignorePaths | ||
); | ||
const renameMap = { | ||
id: "_id", | ||
createdDate: "_createdDate", | ||
updatedDate: "_updatedDate" | ||
}; | ||
return renameAllNestedKeys(payload, renameMap, ignorePaths); | ||
} | ||
@@ -79,0 +73,0 @@ |
{ | ||
"name": "@wix/sdk-runtime", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"license": "UNLICENSED", | ||
@@ -35,2 +35,3 @@ "main": "build/index.js", | ||
"tsup": "^7.3.0", | ||
"type-fest": "^4.8.3", | ||
"typescript": "^5.3.2", | ||
@@ -57,3 +58,3 @@ "vitest": "^0.34.6", | ||
}, | ||
"falconPackageHash": "769a9eeafee09343d050f768e2bbe8aefcab02d18a8aa1ff44e87e92" | ||
"falconPackageHash": "f73bd3714d534cb5d561185cc7669f1292799f741839c1155bff4481" | ||
} |
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
44227
949
11