Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Singular Resource Handling (read/write operations) #98

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
JsonapiResourceIdentifier
} from "./jsonapi-spec"

import { singularize } from "inflected"
import { cloneDeep } from "./util/clonedeep"
import { nonenumerable } from "./util/decorators"
import { IncludeScopeHash } from "./util/include-directive"
Expand All @@ -65,6 +66,7 @@ export interface ModelConfiguration {
keyCase: KeyCase
strictAttributes: boolean
logger: ILogger
singularResource: boolean
}
export type ModelConfigurationOptions = Partial<ModelConfiguration>

Expand Down Expand Up @@ -167,6 +169,7 @@ export class SpraypaintBase {
static keyCase: KeyCase = { server: "snake", client: "camel" }
static strictAttributes: boolean = false
static logger: ILogger = defaultLogger
static singularResource: boolean = false
static sync: boolean = false
static credentials: "same-origin" | "omit" | "include" | undefined
static clientApplication: string | null = null
Expand Down Expand Up @@ -774,10 +777,14 @@ export class SpraypaintBase {
}

static url(id?: string | number): string {
const endpoint = this.endpoint || `/${this.jsonapiType}`
const endpoint =
this.endpoint ||
(this.singularResource
? `/${singularize(this.jsonapiType!)}`
: `/${this.jsonapiType}`)
let base = `${this.fullBasePath()}${endpoint}`

if (id) {
if (id && !this.singularResource) {
base = `${base}/${id}`
}

Expand Down
6 changes: 6 additions & 0 deletions src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export class Scope<T extends SpraypaintBase = SpraypaintBase> {
return this._buildRecordResult(json)
}

async findSingle(): Promise<RecordProxy<T>> {
const json = (await this._fetch(this.model.url())) as JsonapiResourceDoc

return this._buildRecordResult(json)
}

async first(): Promise<RecordProxy<T> | NullProxy<T>> {
const newScope = this.per(1)
let rawResult
Expand Down
2 changes: 1 addition & 1 deletion src/util/parameterize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const parameterize = (obj: any, prefix?: string): string => {

// IE does not encode by default like other browsers
const maybeEncode = (value: string): string => {
const isBrowser = typeof document !== "undefined"
const isBrowser = typeof window !== "undefined"
const isIE = isBrowser && window.navigator.userAgent.match(/(MSIE|Trident)/)
const isEncoded = typeof value === "string" && value.indexOf("%") !== -1
const shouldEncode = isBrowser && isIE && !isEncoded
Expand Down