@tryghost/jsonapi-mapper 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/LICENSE +25 -0
  2. package/README.md +103 -0
  3. package/cjs/bookshelf/extras.js +30 -0
  4. package/cjs/bookshelf/index.js +74 -0
  5. package/cjs/bookshelf/links.js +112 -0
  6. package/cjs/bookshelf/utils.js +168 -0
  7. package/cjs/index.js +20 -0
  8. package/cjs/interfaces/common.js +16 -0
  9. package/cjs/interfaces/index.js +20 -0
  10. package/cjs/interfaces/links.js +16 -0
  11. package/cjs/interfaces/relations.js +16 -0
  12. package/cjs/serializer/index.js +35 -0
  13. package/cjs/serializer/jsonapi-serializer.skel.d.js +1 -0
  14. package/es/bookshelf/extras.js +10 -0
  15. package/es/bookshelf/index.js +55 -0
  16. package/es/bookshelf/links.js +92 -0
  17. package/es/bookshelf/utils.js +171 -0
  18. package/es/index.js +3 -0
  19. package/es/interfaces/common.js +0 -0
  20. package/es/interfaces/index.js +3 -0
  21. package/es/interfaces/links.js +0 -0
  22. package/es/interfaces/relations.js +0 -0
  23. package/es/serializer/index.js +5 -0
  24. package/es/serializer/jsonapi-serializer.skel.d.js +0 -0
  25. package/package.json +88 -0
  26. package/src/bookshelf/extras.ts +77 -0
  27. package/src/bookshelf/index.ts +64 -0
  28. package/src/bookshelf/links.ts +139 -0
  29. package/src/bookshelf/utils.ts +279 -0
  30. package/src/index.ts +3 -0
  31. package/src/interfaces/common.ts +38 -0
  32. package/src/interfaces/index.ts +3 -0
  33. package/src/interfaces/links.ts +26 -0
  34. package/src/interfaces/relations.ts +24 -0
  35. package/src/serializer/index.ts +50 -0
  36. package/src/serializer/jsonapi-serializer.skel.d.ts +4 -0
  37. package/types/bookshelf/extras.d.ts +52 -0
  38. package/types/bookshelf/extras.d.ts.map +1 -0
  39. package/types/bookshelf/index.d.ts +22 -0
  40. package/types/bookshelf/index.d.ts.map +1 -0
  41. package/types/bookshelf/links.d.ts +19 -0
  42. package/types/bookshelf/links.d.ts.map +1 -0
  43. package/types/bookshelf/utils.d.ts +26 -0
  44. package/types/bookshelf/utils.d.ts.map +1 -0
  45. package/types/index.d.ts +4 -0
  46. package/types/index.d.ts.map +1 -0
  47. package/types/interfaces/common.d.ts +24 -0
  48. package/types/interfaces/common.d.ts.map +1 -0
  49. package/types/interfaces/index.d.ts +4 -0
  50. package/types/interfaces/index.d.ts.map +1 -0
  51. package/types/interfaces/links.d.ts +25 -0
  52. package/types/interfaces/links.d.ts.map +1 -0
  53. package/types/interfaces/relations.d.ts +22 -0
  54. package/types/interfaces/relations.d.ts.map +1 -0
  55. package/types/serializer/index.d.ts +34 -0
  56. package/types/serializer/index.d.ts.map +1 -0
@@ -0,0 +1,26 @@
1
+ /**
2
+ * The main purpose of this module is to provide utility functions
3
+ * that follows the restrictions of the Bookshelf/Mapper/Serializer APIs
4
+ * with the goal of simplifying the logic of the main 'map' method.
5
+ */
6
+ import { LinkOpts } from '../interfaces';
7
+ import { SerialOpts } from '../serializer';
8
+ import { BookOpts, Data } from './extras';
9
+ /**
10
+ * Main structure used through most utility and recursive functions
11
+ */
12
+ export interface Information {
13
+ bookOpts: BookOpts;
14
+ linkOpts: LinkOpts;
15
+ }
16
+ /**
17
+ * Start the data processing with top level information,
18
+ * then handle resources recursively in processSample
19
+ */
20
+ export declare function processData(info: Information, data: Data): SerialOpts;
21
+ /**
22
+ * Convert a bookshelf model or collection to
23
+ * json adding the id attribute if missing
24
+ */
25
+ export declare function toJSON(data: Data): any;
26
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/bookshelf/utils.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA2BH,OAAO,EAAE,QAAQ,EAAgB,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAG3C,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAgC,MAAM,UAAU,CAAC;AAExE;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,QAAQ,CAAC;IACnB,QAAQ,EAAE,QAAQ,CAAC;CACpB;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,IAAI,GAAG,UAAU,CAWrE;AAsLD;;;GAGG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,GAAG,CAgCtC"}
@@ -0,0 +1,4 @@
1
+ export * from './bookshelf';
2
+ export * from './interfaces';
3
+ export * from './serializer';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC"}
@@ -0,0 +1,24 @@
1
+ import { PagOpts, QueryOpts } from './links';
2
+ import { RelationTypeOpt, RelationOpts } from './relations';
3
+ export interface Mapper {
4
+ map(data: any, type: string, mapOpts?: MapOpts): any;
5
+ }
6
+ export type AttrMatcher = RegExp | string;
7
+ export type AttributesOpt = {
8
+ omit?: AttrMatcher[];
9
+ include?: AttrMatcher[];
10
+ };
11
+ export interface MapOpts {
12
+ attributes?: AttrMatcher[] | AttributesOpt;
13
+ keyForAttr?: (attr: string) => string;
14
+ relations?: boolean | RelationOpts;
15
+ typeForModel?: RelationTypeOpt;
16
+ enableLinks?: boolean;
17
+ pagination?: PagOpts;
18
+ query?: QueryOpts;
19
+ meta?: {
20
+ [key: string]: any;
21
+ };
22
+ outputVirtuals?: boolean;
23
+ }
24
+ //# sourceMappingURL=common.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/interfaces/common.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAC7C,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAK5D,MAAM,WAAW,MAAM;IACrB,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,GAAG,CAAC;CACtD;AAED,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,MAAM,CAAC;AAE1C,MAAM,MAAM,aAAa,GAAG;IAC1B,IAAI,CAAC,EAAE,WAAW,EAAE,CAAC;IACrB,OAAO,CAAC,EAAE,WAAW,EAAE,CAAA;CACxB,CAAC;AAGF,MAAM,WAAW,OAAO;IAEtB,UAAU,CAAC,EAAE,WAAW,EAAE,GAAG,aAAa,CAAC;IAC3C,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;IAGtC,SAAS,CAAC,EAAE,OAAO,GAAG,YAAY,CAAC;IACnC,YAAY,CAAC,EAAE,eAAe,CAAC;IAG/B,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,KAAK,CAAC,EAAE,SAAS,CAAC;IAGlB,IAAI,CAAC,EAAE;QAAE,CAAC,GAAG,EAAC,MAAM,GAAG,GAAG,CAAA;KAAE,CAAC;IAG7B,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B"}
@@ -0,0 +1,4 @@
1
+ export * from './common';
2
+ export * from './links';
3
+ export * from './relations';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Query Parameters map
3
+ */
4
+ export interface QueryOpts {
5
+ [key: string]: string;
6
+ }
7
+ /**
8
+ * Pagination variables
9
+ */
10
+ export interface PagOpts {
11
+ offset: number;
12
+ limit: number;
13
+ total?: number;
14
+ rowCount?: number;
15
+ }
16
+ /**
17
+ * Data required to form links
18
+ */
19
+ export interface LinkOpts {
20
+ baseUrl: string;
21
+ type: string;
22
+ pag?: PagOpts;
23
+ query?: QueryOpts;
24
+ }
25
+ //# sourceMappingURL=links.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"links.d.ts","sourceRoot":"","sources":["../../src/interfaces/links.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,KAAK,CAAC,EAAE,SAAS,CAAC;CACnB"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Map to specify type for the passed relations
3
+ */
4
+ export interface RelationTypeMap {
5
+ [relationName: string]: string;
6
+ }
7
+ /**
8
+ * Function to pass directly as the typeForAttributes option to the serializer
9
+ */
10
+ export type RelationTypeFunction = (attribute: string) => string;
11
+ /**
12
+ * The relationTypes option can be a function or an object
13
+ */
14
+ export type RelationTypeOpt = RelationTypeMap | RelationTypeFunction;
15
+ /**
16
+ * Relationship options
17
+ */
18
+ export interface RelationOpts {
19
+ included: boolean | string[];
20
+ fields?: string[];
21
+ }
22
+ //# sourceMappingURL=relations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"relations.d.ts","sourceRoot":"","sources":["../../src/interfaces/relations.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAAC;CAChC;AAED;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG,CAAC,SAAS,EAAE,MAAM,KAAK,MAAM,CAAC;AAEjE;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,eAAe,GAAG,oBAAoB,CAAC;AAErE;;GAEG;AACH,MAAM,WAAW,YAAY;IACzB,QAAQ,EAAE,OAAO,GAAG,MAAM,EAAE,CAAC;IAC7B,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB"}
@@ -0,0 +1,34 @@
1
+ export type LinkFunc = (primary: any, related?: any, parent?: any) => string;
2
+ export type Link = string | LinkFunc;
3
+ export interface LinkObj {
4
+ self?: Link;
5
+ related?: Link;
6
+ first?: Link;
7
+ last?: Link;
8
+ prev?: Link;
9
+ next?: Link;
10
+ }
11
+ export interface SerialOpts {
12
+ attributes?: string[];
13
+ ref?: string;
14
+ included?: boolean;
15
+ topLevelLinks?: LinkObj;
16
+ dataLinks?: LinkObj;
17
+ relationshipLinks?: LinkObj;
18
+ includedLinks?: LinkObj;
19
+ relationshipMeta?: any;
20
+ ignoreRelationshipData?: boolean;
21
+ keyForAttribute?: (attribute: any) => string;
22
+ typeForAttribute?: (attribute: any) => any;
23
+ pluralizeType?: boolean;
24
+ meta?: any;
25
+ [relationships: string]: any;
26
+ }
27
+ export interface SerializerCtor {
28
+ new (type: string, opts: SerialOpts): Serializer;
29
+ }
30
+ export interface Serializer {
31
+ serialize(data: any): any;
32
+ }
33
+ export declare let Serializer: SerializerCtor;
34
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/serializer/index.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,EA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