summary refs log tree commit diff
diff options
context:
space:
mode:
authorPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2020-01-28 12:18:47 +0100
committerPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2020-01-28 12:18:47 +0100
commit74ee8edb555ef128112378f828c636bc04533b31 (patch)
treefe4a09d720f9437bc0dc418180c9cf11f4d0a14b
parent9f75ff1d2d5ebf34c040474e836e125f80253365 (diff)
downloadkronikarz-74ee8edb555ef128112378f828c636bc04533b31.tar.gz
kronikarz-74ee8edb555ef128112378f828c636bc04533b31.zip
Fix remove attributes
-rw-r--r--lib/Post.ts9
-rw-r--r--lib/interfaces.ts10
-rw-r--r--test/generateApi.spec.ts2
-rw-r--r--test/getPost.spec.ts3
4 files changed, 14 insertions, 10 deletions
diff --git a/lib/Post.ts b/lib/Post.ts
index 27a6cb9..f99ef83 100644
--- a/lib/Post.ts
+++ b/lib/Post.ts
@@ -38,13 +38,16 @@ export default class Post {
   getMeta(): Meta {
     const { attributes } = this.post;
     const author: string = attributes.author;
-    delete attributes.author;
+    if (!author) throw 'Error while parsing the author';
     const title: string = attributes.title;
-    delete attributes.title;
+    if (!title) throw 'Error while parsing the title';
+    const additionalMeta = Object.keys(attributes)
+      .filter((key) => key != 'title' && key != 'author')
+      .reduce((acc, key) => ({ ...acc, [key]: attributes[key] }), {});
     return {
       author,
       title,
-      additionalMeta: attributes,
+      additionalMeta,
     };
   }
 
diff --git a/lib/interfaces.ts b/lib/interfaces.ts
index 974765f..158ac1d 100644
--- a/lib/interfaces.ts
+++ b/lib/interfaces.ts
@@ -4,19 +4,19 @@ export interface Date {
   day: string;
 }
 
-/*export interface Meta {
-  [key: string]: object;
-}*/
+export interface Object {
+  [key: string]: any;
+}
 
 export interface Meta {
   title: string;
   author: string;
-    additionalMeta: object;
+  additionalMeta: object;
 }
 
 export interface FrontMatterObject {
   body: string;
-  attributes: Meta;
+  attributes: Object;
 }
 
 export interface PostApiListEntry {
diff --git a/test/generateApi.spec.ts b/test/generateApi.spec.ts
index c2a96e3..bf63b6e 100644
--- a/test/generateApi.spec.ts
+++ b/test/generateApi.spec.ts
@@ -8,6 +8,6 @@ test("generate single file", () => {
   k.generateApi(dir);
   fs.readFile("./tmp/api/posts/2019/12/12/test.json", "utf-8", (err, data) => {
     const parsedPost = JSON.parse(data);
-    expect(parsedPost.title).toBe("test");
+    expect(parsedPost.title).toBe("Test");
   });
 });
diff --git a/test/getPost.spec.ts b/test/getPost.spec.ts
index 72d7b5e..080bfbd 100644
--- a/test/getPost.spec.ts
+++ b/test/getPost.spec.ts
@@ -3,6 +3,7 @@ import Kronikarz from "../dist";
 const k = new Kronikarz(__dirname + "/samples");
 
 test("simple get", () => {
-  const post = k.getPost("2019", "12", "12", "test");
+  const p = k.getPost("2019", "12", "12", "test");
+    const post = p.toApi();
   expect(post.title).toBe("Test");
 });