Skip to content

Commit ca0c317

Browse files
Exelordebryn
authored andcommitted
Feature/custom types names (#45)
* Allow to define custom type name * Fix no attributes issue * Fix strings util
1 parent dc39eaf commit ca0c317

File tree

6 files changed

+37
-36
lines changed

6 files changed

+37
-36
lines changed

src/middlewares/json-api-koa.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@ import { decode } from "jsonwebtoken";
33
import { Context, Middleware } from "koa";
44
import * as koaBody from "koa-body";
55
import * as compose from "koa-compose";
6-
76
import Application from "../application";
87
import JsonApiErrors from "../json-api-errors";
9-
import {
10-
JsonApiDocument,
11-
JsonApiError,
12-
JsonApiErrorsDocument,
13-
Operation,
14-
OperationResponse
15-
} from "../types";
8+
import { JsonApiDocument, JsonApiError, JsonApiErrorsDocument, Operation, OperationResponse } from "../types";
169
import { parse } from "../utils/json-api-params";
17-
import { classify, singularize } from "../utils/string";
10+
import { camelize, singularize } from "../utils/string";
1811

1912
const STATUS_MAPPING = {
2013
GET: 200,
@@ -38,9 +31,9 @@ export default function jsonApiKoa(
3831
return await next();
3932
}
4033

41-
const typeNames = app.types.map(t => t.name);
34+
const typeNames = app.types.map(t => t.type);
4235

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

118111
function convertHttpRequestToOperation(ctx: Context): Operation {
119112
const { id, resource } = ctx.urlData;
120-
const type = classify(singularize(resource));
113+
const type = camelize(singularize(resource));
121114

122115
const opMap = {
123116
GET: "get",

src/processors/knex-processor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as Knex from "knex";
2-
32
import Resource from "../resource";
43
import { KnexRecord, Operation, ResourceConstructor } from "../types";
54
import { camelize, pluralize } from "../utils/string";
6-
75
import OperationProcessor from "./operation-processor";
86

7+
8+
99
const operators = {
1010
eq: "=",
1111
ne: "!=",
@@ -63,7 +63,7 @@ export default class KnexProcessor<
6363
const resource = Object.create(this.resourceFor(type));
6464
const fields = params ? { ...params.fields } : {};
6565
const attributes = getAttributes(
66-
Object.keys(resource.__proto__.attributes),
66+
Object.keys(resource.__proto__.attributes || {}),
6767
fields,
6868
type
6969
);

src/processors/operation-processor.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import Application from "../application";
22
import Resource from "../resource";
33
import { Operation, ResourceConstructor } from "../types";
4-
import { classify, singularize } from "../utils/string";
4+
import { camelize, singularize } from "../utils/string";
55

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

1010
shouldHandle(op: Operation) {
11-
return this.resourceClass && op.ref.type === this.resourceClass.name;
11+
return this.resourceClass && op.ref.type === this.resourceClass.type;
1212
}
1313

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

19-
resourceFor(type: string = ""): ResourceConstructor {
20-
return this.app.types.find(({ name }: { name: string }) => {
21-
return name === classify(singularize(type));
19+
resourceFor(resourceType: string = ""): ResourceConstructor {
20+
return this.app.types.find(({ type }: { type: string }) => {
21+
return type === camelize(singularize(resourceType));
2222
});
2323
}
2424

src/resource.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ import { ResourceTypeAttributes, ResourceTypeRelationships } from "./types";
22
import { camelize } from "./utils/string";
33

44
export default abstract class Resource {
5-
public type: string;
6-
public id?: string;
7-
public attributes: ResourceTypeAttributes;
8-
public relationships: ResourceTypeRelationships;
5+
id?: string;
6+
type: string;
7+
attributes: ResourceTypeAttributes;
8+
relationships: ResourceTypeRelationships;
9+
10+
static get type() {
11+
return camelize(this.name);
12+
}
913

1014
constructor({
1115
id,
@@ -16,9 +20,8 @@ export default abstract class Resource {
1620
attributes?: ResourceTypeAttributes;
1721
relationships?: ResourceTypeRelationships;
1822
}) {
19-
this.type = camelize(this.constructor.name);
20-
2123
this.id = id;
24+
this.type = (this.constructor as typeof Resource).type;
2225
this.attributes = attributes || {};
2326
this.relationships = relationships || {};
2427
}

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ export type OperationResponse = {
9898
};
9999

100100
export type ResourceConstructor<ResourceT = Resource> = {
101+
type: string;
102+
101103
new ({
102104
id,
103105
attributes,

src/utils/string.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
export { plural as pluralize, singular as singularize } from "pluralize";
2-
3-
export {
4-
decamelize,
5-
dasherize,
6-
camelize,
7-
classify,
8-
underscore,
9-
capitalize
10-
} from "ember-cli-string-utils";
1+
import { plural, singular } from "pluralize";
2+
3+
function pluralize(word = "") {
4+
return plural(word);
5+
}
6+
7+
function singularize(word = "") {
8+
return singular(word);
9+
}
10+
11+
export { camelize, capitalize, classify, dasherize, decamelize, underscore } from "ember-cli-string-utils";
12+
export { pluralize, singularize };
13+

0 commit comments

Comments
 (0)