Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Resource

Hierarchy

  • Resource

Index

Constructors

constructor

  • new Resource(attributes?: any, options?: any): Resource
  • Parameters

    • Default value attributes: any = {}
    • Default value options: any = {}

    Returns Resource

Properties

_attributes

_attributes: Record<string, any>

attributes

attributes: Record<string, any>

changes

changes: Record<string, any>

managers

managers: Record<string, RelatedManager>

uuid

uuid: string

Static RelatedManagerClass

RelatedManagerClass: RelatedManager = RelatedManager

Static _cache

_cache: any

Static _uuid

_uuid: string

Static cacheMaxAge

cacheMaxAge: number = 10

Static client

client: DefaultClient = new DefaultClient('/')

Static defaults

defaults: Record<string, any>

Static endpoint

endpoint: string = ""

Static fields

fields: string[] = []

Static normalization

normalization: NormalizerDict

Static queued

queued: Record<string, any>

Static related

Static uniqueKey

uniqueKey: string = "id"

Static validation

Accessors

id

  • get id(): string
  • set id(value: string): void

Static cache

  • get cache(): any

Static uuid

  • get uuid(): string

Static validators

  • get validators(): any
  • set validators(value: any): void
  • Backwards compatibility Remove in next major release @todo

    Returns any

  • Backwards compatibility Remove in next major release @todo

    Parameters

    • value: any

    Returns void

Methods

cache

  • cache<T>(this: T, replace?: boolean): T
  • Type parameters

    Parameters

    • this: T
    • Default value replace: boolean = false

    Returns T

createManagerFor

delete

get

  • get<T>(key?: string): T
  • Get an attribute of Resource instance You can use dot notation here -- eg. resource.get('user.username') You can also get all properties by not providing any arguments

    param?

    key

    Type parameters

    • T

    Parameters

    • Optional key: string

    Returns T

getAsync

  • getAsync<T>(key: string): Promise<T>
  • Alias of resource.resolveAttribute(key)

    Type parameters

    • T

    Parameters

    • key: string

    Returns Promise<T>

getCached

getConstructor

  • getConstructor<T>(): T
  • Like calling instance.constructor but safer: changing objects down the line won't creep up the prototype chain and end up on native global objects like Function or Object

    Type parameters

    Returns T

isNew

  • isNew(): boolean

rel

resolveAttribute

  • resolveAttribute<T>(key: string): Promise<T>
  • Persist getting an attribute and get related keys until a key can be found (or not found) TypeError in get() will be thrown, we're just doing the resolveRelated() work for you...

    Type parameters

    • T

    Parameters

    • key: string

    Returns Promise<T>

resolveRelated

  • resolveRelated(__namedParameters?: object): Promise<void>
  • Match all related values in attributes[key] where key is primary key of related instance defined in Resource.related[key]

    Parameters

    • Default value __namedParameters: object = {}

    Returns Promise<void>

resolveRelatedDeep

  • Same as Resource.prototype.resolveRelated except options.deep defaults to true

    Parameters

    Returns Promise<void>

save

  • Saves the instance -- sends changes as a PATCH or sends whole object as a POST if it's new

    Type parameters

    • T: this

    Parameters

    Returns Promise<ResourceResponse<T>>

set

  • set(key: string, value: any): this
  • Set an attribute of Resource instance and apply getters/setters Do not use Dot Notation here

    Parameters

    • key: string
    • value: any

    Returns this

toInternalValue

  • toInternalValue(key: string, value: any): any
  • Setter -- Translate new value into an internal value onto this._attributes[key] Usually this is just setting a key/value but we want to be able to accept anything -- another Resource instance for example. If a Resource instance is provided, set the this.managers[key] as the new manager instance, then set the this.attributes[key] field as just the primary key of the related Resource instance

    Parameters

    • key: string
    • value: any

    Returns any

toJSON

  • toJSON(): any

toResourceName

  • toResourceName(): string

toString

  • toString(): string

update

  • update<T>(this: T): Promise<T>

validate

  • validate(): Error[]
  • Validate attributes -- returns empty if no errors exist -- you should throw new errors here

    Returns Error[]

    Error[] Array of Exceptions

wrap

  • wrap(relativePath: string, query?: any): object
  • Parameters

    • relativePath: string
    • Optional query: any

    Returns object

Static cacheDeltaSeconds

  • cacheDeltaSeconds(): number
  • Get time delta in seconds of cache expiry

    Returns number

Static cacheResource

  • cacheResource<T>(this: T, resource: InstanceType<T>, replace?: boolean): void
  • Cache a resource onto this class' cache for cacheMaxAge seconds

    Type parameters

    Parameters

    • this: T
    • resource: InstanceType<T>
    • Default value replace: boolean = false

    Returns void

Static clearCache

  • clearCache(): void

Static detail

  • detail<T>(this: T, id: string | number, options?: DetailOpts): Promise<InstanceType<T>>
  • Type parameters

    Parameters

    • this: T
    • id: string | number
    • Default value options: DetailOpts = {}

    Returns Promise<InstanceType<T>>

Static extend

  • extend<T, U>(this: U, classProps: T): U & T
  • Type parameters

    • T

    • U

    Parameters

    • this: U
    • classProps: T

    Returns U & T

Static getCached

  • getCached<T>(this: T, id: string | number): CachedResource<InstanceType<T>> | undefined
  • Get a cached resource by ID

    Type parameters

    Parameters

    • this: T
    • id: string | number

    Returns CachedResource<InstanceType<T>> | undefined

Static getCachedAll

Static getDetailRoutePath

  • getDetailRoutePath(id: string | number, query?: any): string
  • Get detail route path (eg. /users/123) to be used with HTTP requests

    Parameters

    • id: string | number
    • Optional query: any

      Querystring

    Returns string

Static getListRoutePath

  • getListRoutePath(query?: any): string
  • Get list route path (eg. /users) to be used with HTTP requests and allow a querystring object

    Parameters

    • Optional query: any

      Querystring

    Returns string

Static getResourceHashKey

  • getResourceHashKey(resourceId: string | number): string
  • Unique resource hash key used for caching and organizing requests

    Parameters

    • resourceId: string | number

    Returns string

Static list

  • HTTP Get of resource's list route--returns a promise

    Type parameters

    Parameters

    • this: T
    • Default value options: ListOpts = {}

      Options object

    Returns ListResponse<T>

    Promise

Static makeDefaultsObject

  • makeDefaultsObject(): any

Static replaceCache

  • replaceCache<T>(resource: T): void
  • Replace attributes on a cached resource onto this class' cache for cacheMaxAge seconds (useful for bubbling up changes to states that may be already rendered)

    Type parameters

    Parameters

    • resource: T

    Returns void

Static toResourceName

  • toResourceName(): string

Static wrap

  • wrap(relativePath: string, query?: any): object
  • Parameters

    • relativePath: string
    • Optional query: any

    Returns object

Generated using TypeDoc