Skip to content

Commit

Permalink
Inject token key option (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroki0525 authored Dec 8, 2023
1 parent da91b84 commit c4fff7f
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 33 deletions.
6 changes: 6 additions & 0 deletions .changeset/purple-foxes-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@dandori/libs": patch
"@dandori/ui": patch
---

add inject api key option
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"prettier": "3.1.0",
"tsup": "^8.0.1",
"tsx": "^4.6.2",
"turbo": "^1.11.0",
"turbo": "^1.11.1",
"typescript": "5.3.3",
"vitest": "^0.34.6"
},
Expand Down
40 changes: 40 additions & 0 deletions packages/libs/src/__tests__/checkApiKey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, it, expect } from "vitest";
import { checkApiKey } from "../index";

describe("checkApiKey", () => {
const keyName = "keyName";
const targetKey = "targetKey";
const alternativeKey = "alternativeKey";

describe("with targetKey", () => {
describe("with alternativeKey", () => {
it("returns targetKey", () => {
expect(checkApiKey(keyName, targetKey, alternativeKey)).toBe(targetKey);
});
});

describe("without alternativeKey", () => {
it("returns targetKey", () => {
expect(checkApiKey(keyName, targetKey, undefined)).toBe(targetKey);
});
});
});

describe("without targetKey", () => {
describe("with alternativeKey", () => {
it("returns alternativeKey", () => {
expect(checkApiKey(keyName, undefined, alternativeKey)).toBe(
alternativeKey,
);
});
});

describe("without alternativeKey", () => {
it("throw error", () => {
expect(() => checkApiKey(keyName, undefined, undefined)).toThrow(
`Please set ${keyName}.`,
);
});
});
});
});
13 changes: 13 additions & 0 deletions packages/libs/src/checkApiKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function checkApiKey(
keyName: string,
targetKey?: string,
alternativeKey?: string,
): string {
if (targetKey) {
return targetKey;
}
if (alternativeKey) {
return alternativeKey;
}
throw new Error(`Please set ${keyName}.`);
}
1 change: 1 addition & 0 deletions packages/libs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./loadFile";
export * from "./loadEnvFile";
export * from "./logger";
export * from "./runPromisesSequentially";
export * from "./checkApiKey";
14 changes: 13 additions & 1 deletion packages/ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ The tasks which are generated by `generateDandoriTasks` of `@dandori/core`.
interface GenerateDandoriMiroCardsOptions {
boardId: string;
isAppCard?: boolean;
apiKey?: string;
}
```

Expand All @@ -84,6 +85,12 @@ For example, if the url is `https://miro.com/app/board/1234567890/`, the board i

If you set `true` , the cards are created as [App cards](https://developers.miro.com/docs/app-card).

* apiKey

The api key of miro. You can also set `MIRO_API_KEY` environment variable instead of this option.

```ts

### generateDandoriNotionTasks

```ts
Expand All @@ -109,6 +116,7 @@ The tasks which are generated by `generateDandoriTasks` of `@dandori/core`.
interface GenerateDandoriNotionPagesOptions {
databaseId: string;
databasePropertiesMap?: DatabasePropertiesMap;
apuKey?: string;
}
```

Expand Down Expand Up @@ -169,4 +177,8 @@ This is an example. In this case, the output is like belows.

<img src="../../media/notion_example.png" alt="notion output example" width="426">

For more details about database properties, please see [Notion API](https://developers.notion.com/reference/page#page-property-value).
For more details about database properties, please see [Notion API](https://developers.notion.com/reference/page#page-property-value).

* apiKey

The api key of miro. You can also set `NOTION_API_KEY` environment variable instead of this option.
1 change: 1 addition & 0 deletions packages/ui/src/__tests__/miro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ vi.mock("@dandori/libs", () => {
runPromisesSequentially: vi.fn((runPromises, _runningLogPrefix) =>
Promise.all(runPromises.map((runPromise: () => any) => runPromise())),
),
checkApiKey: vi.fn(),
};
});

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/__tests__/notion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ vi.mock("@dandori/libs", () => {
runPromisesSequentially: vi.fn((runPromises, _runningLogPrefix) =>
Promise.all(runPromises.map((runPromise: () => any) => runPromise())),
),
checkApiKey: vi.fn(),
};
});

Expand Down
21 changes: 14 additions & 7 deletions packages/ui/src/miro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import { MiroApi } from "@mirohq/miro-api";
import { DandoriTask } from "@dandori/core";
import FlatToNested from "flat-to-nested";
import TreeModel from "tree-model";
import { getLogger, getLogLevel, runPromisesSequentially } from "@dandori/libs";
import {
checkApiKey,
getLogger,
getLogLevel,
runPromisesSequentially,
} from "@dandori/libs";

export type GenerateDandoriMiroCardsOptions = {
boardId: Parameters<MiroApi["getBoard"]>[0];
isAppCard?: boolean;
apiKey?: string;
};

// miro settings
Expand Down Expand Up @@ -53,14 +59,15 @@ export async function generateDandoriMiroCards(
tasks: DandoriTask[],
options: GenerateDandoriMiroCardsOptions,
): Promise<void> {
const logger = getLogger();
const miroApi = new MiroApi(
const key = checkApiKey(
"miro api key",
process.env.MIRO_API_KEY,
undefined,
(...thing) => {
logger[getLogLevel()](thing);
},
options.apiKey,
);
const logger = getLogger();
const miroApi = new MiroApi(key, undefined, (...thing) => {
logger[getLogLevel()](thing);
});
const miroBoard = await miroApi.getBoard(options.boardId);
const taskFlat: (DandoriTask & { [taskParentPropName]?: string })[] = tasks
.map((task) =>
Expand Down
15 changes: 13 additions & 2 deletions packages/ui/src/notion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {
DandoriTaskStatus,
} from "@dandori/core";
import { Client, LogLevel } from "@notionhq/client";
import { getLogger, getLogLevel, runPromisesSequentially } from "@dandori/libs";
import {
checkApiKey,
getLogger,
getLogLevel,
runPromisesSequentially,
} from "@dandori/libs";

type SupportNotionTaskOptionalProperty = Exclude<
DandoriTaskOptionalProperty,
Expand Down Expand Up @@ -56,6 +61,7 @@ export type DatabasePropertiesMap =
export type GenerateDandoriNotionPagesOptions = {
databaseId: string;
databasePropertiesMap?: DatabasePropertiesMap;
apiKey?: string;
};

const hasStatusProperty = (
Expand Down Expand Up @@ -145,9 +151,14 @@ export async function generateDandoriNotionPages(
tasks: DandoriTask[],
options: GenerateDandoriNotionPagesOptions,
): Promise<void> {
const key = checkApiKey(
"notion api key",
process.env.NOTION_API_KEY,
options.apiKey,
);
const logger = getLogger();
const client = new Client({
auth: process.env.NOTION_API_KEY,
auth: key,
logLevel: getNotionLogLevel(),
logger: (
level: LogLevel,
Expand Down
44 changes: 22 additions & 22 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c4fff7f

Please sign in to comment.