Skip to content

Commit

Permalink
Feature/custom types names (#45)
Browse files Browse the repository at this point in the history
* Allow to define custom type name

* Fix no attributes issue

* Fix strings util
  • Loading branch information
Exelord authored and ebryn committed Mar 22, 2019
1 parent dc39eaf commit ca0c317
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 36 deletions.
17 changes: 5 additions & 12 deletions src/middlewares/json-api-koa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@ import { decode } from "jsonwebtoken";
import { Context, Middleware } from "koa";
import * as koaBody from "koa-body";
import * as compose from "koa-compose";

import Application from "../application";
import JsonApiErrors from "../json-api-errors";
import {
JsonApiDocument,
JsonApiError,
JsonApiErrorsDocument,
Operation,
OperationResponse
} from "../types";
import { JsonApiDocument, JsonApiError, JsonApiErrorsDocument, Operation, OperationResponse } from "../types";
import { parse } from "../utils/json-api-params";
import { classify, singularize } from "../utils/string";
import { camelize, singularize } from "../utils/string";

const STATUS_MAPPING = {
GET: 200,
Expand All @@ -38,9 +31,9 @@ export default function jsonApiKoa(
return await next();
}

const typeNames = app.types.map(t => t.name);
const typeNames = app.types.map(t => t.type);

if (typeNames.includes(classify(singularize(data.resource)))) {
if (typeNames.includes(camelize(singularize(data.resource)))) {
ctx.urlData = data;
return await handleJsonApiEndpoints(app, ctx).then(() => next());
}
Expand Down Expand Up @@ -117,7 +110,7 @@ async function handleJsonApiEndpoints(app: Application, ctx: Context) {

function convertHttpRequestToOperation(ctx: Context): Operation {
const { id, resource } = ctx.urlData;
const type = classify(singularize(resource));
const type = camelize(singularize(resource));

const opMap = {
GET: "get",
Expand Down
6 changes: 3 additions & 3 deletions src/processors/knex-processor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as Knex from "knex";

import Resource from "../resource";
import { KnexRecord, Operation, ResourceConstructor } from "../types";
import { camelize, pluralize } from "../utils/string";

import OperationProcessor from "./operation-processor";



const operators = {
eq: "=",
ne: "!=",
Expand Down Expand Up @@ -63,7 +63,7 @@ export default class KnexProcessor<
const resource = Object.create(this.resourceFor(type));
const fields = params ? { ...params.fields } : {};
const attributes = getAttributes(
Object.keys(resource.__proto__.attributes),
Object.keys(resource.__proto__.attributes || {}),
fields,
type
);
Expand Down
10 changes: 5 additions & 5 deletions src/processors/operation-processor.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import Application from "../application";
import Resource from "../resource";
import { Operation, ResourceConstructor } from "../types";
import { classify, singularize } from "../utils/string";
import { camelize, singularize } from "../utils/string";

export default class OperationProcessor<ResourceT = Resource> {
public app: Application;
public resourceClass?: ResourceConstructor;

shouldHandle(op: Operation) {
return this.resourceClass && op.ref.type === this.resourceClass.name;
return this.resourceClass && op.ref.type === this.resourceClass.type;
}

execute(op: Operation): Promise<ResourceT | ResourceT[] | void> {
const action: string = op.op;
return this[action] && this[action].call(this, op);
}

resourceFor(type: string = ""): ResourceConstructor {
return this.app.types.find(({ name }: { name: string }) => {
return name === classify(singularize(type));
resourceFor(resourceType: string = ""): ResourceConstructor {
return this.app.types.find(({ type }: { type: string }) => {
return type === camelize(singularize(resourceType));
});
}

Expand Down
15 changes: 9 additions & 6 deletions src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import { ResourceTypeAttributes, ResourceTypeRelationships } from "./types";
import { camelize } from "./utils/string";

export default abstract class Resource {
public type: string;
public id?: string;
public attributes: ResourceTypeAttributes;
public relationships: ResourceTypeRelationships;
id?: string;
type: string;
attributes: ResourceTypeAttributes;
relationships: ResourceTypeRelationships;

static get type() {
return camelize(this.name);
}

constructor({
id,
Expand All @@ -16,9 +20,8 @@ export default abstract class Resource {
attributes?: ResourceTypeAttributes;
relationships?: ResourceTypeRelationships;
}) {
this.type = camelize(this.constructor.name);

this.id = id;
this.type = (this.constructor as typeof Resource).type;
this.attributes = attributes || {};
this.relationships = relationships || {};
}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export type OperationResponse = {
};

export type ResourceConstructor<ResourceT = Resource> = {
type: string;

new ({
id,
attributes,
Expand Down
23 changes: 13 additions & 10 deletions src/utils/string.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
export { plural as pluralize, singular as singularize } from "pluralize";

export {
decamelize,
dasherize,
camelize,
classify,
underscore,
capitalize
} from "ember-cli-string-utils";
import { plural, singular } from "pluralize";

function pluralize(word = "") {
return plural(word);
}

function singularize(word = "") {
return singular(word);
}

export { camelize, capitalize, classify, dasherize, decamelize, underscore } from "ember-cli-string-utils";
export { pluralize, singularize };

0 comments on commit ca0c317

Please sign in to comment.