Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add JSR support #110

Merged
merged 9 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Publish

on:
push:
tags:
- '*'

jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write # The OIDC ID token is used for authentication with JSR.
steps:
- uses: actions/checkout@v4
- run: npx jsr publish
1 change: 1 addition & 0 deletions .github/workflows/samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
--allow-read --allow-write --allow-net
deno-slack-api/scripts/import_map/update.ts
--import-map "./sample/import_map.json"
--parent-import-map "./deno-slack-api/deno.jsonc"
--api "../deno-slack-api/src/"

- name: Deno check **/*.ts
Expand Down
54 changes: 29 additions & 25 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
{
"$schema": "https://deno.land/x/deno/cli/schemas/config-file.v1.json",
"fmt": {
"files": {
"include": [
"src",
"scripts",
"docs",
"README.md",
".github/maintainers_guide.md",
".github/CONTRIBUTING.md"
]
},
"options": {
"semiColons": true,
"indentWidth": 2,
"lineWidth": 80,
"proseWrap": "always",
"singleQuote": false,
"useTabs": false
}
"include": [
"src",
"scripts",
"docs",
"README.md",
".github/maintainers_guide.md",
".github/CONTRIBUTING.md"
]
},
"lint": {
"files": {
"include": ["src", "scripts"]
}
"include": ["src", "scripts"]
},
"publish": {
"exclude": ["./README.md"]
},
"test": {
"files": {
"include": ["src", "scripts"]
}
"include": ["src", "scripts"]
},
"tasks": {
"test": "deno fmt --check && deno lint && deno test",
"generate-lcov": "rm -rf .coverage && deno test --reporter=dot --coverage=.coverage && deno coverage --exclude=fixtures --exclude=test --exclude=scripts --exclude=src/generated --lcov --output=lcov.info .coverage",
"test:coverage": "deno task generate-lcov && deno coverage --exclude=fixtures --exclude=test --exclude=scripts --exclude=src/generated .coverage src"
},
"lock": false
"lock": false,
"name": "@slack/api",
"version": "2.9.0",
"exports": {
".": "./src/mod.ts",
"./types": "./src/types.ts"
},
"imports": {
"@deno/dnt": "jsr:@deno/dnt@^0.41",
"@std/assert": "jsr:@std/assert@^1",
"@std/cli": "jsr:@std/cli@^1",
"@std/fs": "jsr:@std/fs@^1",
"@std/http": "jsr:@std/http@^0.206",
"@std/testing": "jsr:@std/testing@^1",
"@std/text": "jsr:@std/text@^1"
}
}
2 changes: 1 addition & 1 deletion docs/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ To instantiate the Slack API Client, use the top level `SlackAPI` export.
default this is set to `"https://slack.com/api/"`

```ts
import { SlackAPI } from "https://deno.land/x/[email protected]/mod.ts";
import { SlackAPI } from "jsr:@slack/api";

// create a client with defaults
const client = SlackAPI(token);
Expand Down
5 changes: 3 additions & 2 deletions scripts/build_npm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ex. scripts/build_npm.ts
import { build, emptyDir } from "https://deno.land/x/[email protected]/mod.ts";
import { emptyDir } from "@std/fs";
import { build } from "@deno/dnt";

await emptyDir("./npm");

Expand All @@ -10,7 +11,7 @@ await build({
outDir: "./npm",
// ensures that the emitted package is compatible with node v14 later
compilerOptions: {
lib: ["es2022.error"], // fix ErrorOptions not exported in ES2020
lib: ["ES2022.Error"], // fix ErrorOptions not exported in ES2020
target: "ES2020",
},
shims: {
Expand Down
26 changes: 18 additions & 8 deletions scripts/import_map/update.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import {
createHttpError,
} from "https://deno.land/[email protected]/http/http_errors.ts";
import { parseArgs } from "@std/cli/parse-args";
import { createHttpError } from "@std/http/http-errors";

// Regex for https://deno.land/x/[email protected]/
const API_REGEX =
/(https:\/\/deno.land\/x\/deno_slack_api@[0-9]+\.[0-9]+\.[0-9]+\/)/g;
/(https:\/\/deno.land\/x\/deno_slack_api@[0-9]\.[0-9]+\.[0-9]+\/)/g;

async function main() {
const flags = parse(Deno.args, {
string: ["import-map", "api"],
const flags = parseArgs(Deno.args, {
string: ["import-map", "api", "parent-import-map"],
default: {
"import-map": `${Deno.cwd()}/import_map.json`,
"api": "../deno-slack-api/src/",
"parent-import-map": undefined,
},
});

Expand All @@ -37,7 +36,18 @@ async function main() {
),
};

const importMapJsonOut = JSON.stringify(importMap);
if (flags["parent-import-map"]) {
const parentImportMapJsonIn = await Deno.readTextFile(
flags["parent-import-map"],
);
console.log("parent `import_map.json` in content:", parentImportMapJsonIn);
const parentImportMap = JSON.parse(parentImportMapJsonIn);
for (const entry of Object.entries(parentImportMap["imports"])) {
importMap["imports"][entry[0]] = entry[1];
}
}

const importMapJsonOut = JSON.stringify(importMap, null, 2);
console.log("`import_map.json` out content:", importMapJsonOut);

await Deno.writeTextFile(flags["import-map"], importMapJsonOut);
Expand Down
22 changes: 7 additions & 15 deletions scripts/import_map/update_test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { HttpError } from "../../src/deps.ts";
import {
afterEach,
assertEquals,
assertRejects,
beforeAll,
mf,
} from "../../src/dev_deps.ts";
import { isHttpError } from "@std/http/http-errors";
import { mf } from "../../src/dev_deps.ts";
import { assertEquals, assertRejects } from "@std/assert";
import { afterEach, beforeAll } from "@std/testing/bdd";
import { apiDepsIn } from "./update.ts";

const depsTsMock =
Expand Down Expand Up @@ -47,12 +43,8 @@ Deno.test("apiDepsIn should throw http error on response not ok", async () => {
return new Response("error", { status: 500 });
});

await assertRejects(
async () => {
return await apiDepsIn(
"https://deno.land/x/[email protected]/",
);
},
HttpError,
const error = await assertRejects(() =>
apiDepsIn("https://deno.land/x/[email protected]/")
);
isHttpError(error);
});
9 changes: 5 additions & 4 deletions scripts/src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { emptyDir, ensureDir, pascalCase } from "../../src/deps.ts";
import { toPascalCase } from "@std/text/to-pascal-case";
import { emptyDir, ensureDir } from "@std/fs";
import { APIMethodNode } from "./api-method-node.ts";
import { getPublicAPIMethods } from "./public-api-methods.ts";

Expand Down Expand Up @@ -67,12 +68,12 @@ run();

const getMainAPICode = (api: APIMethodNode): string => {
const imports = api.childNodes.map((node) => {
const groupAPITypeName = `${pascalCase(node.name)}APIType`;
const groupAPITypeName = `${toPascalCase(node.name)}APIType`;
return `import { type ${groupAPITypeName} } from "./${node.name}.ts";`;
}).join("\n");

const apiTypeMixins = api.childNodes.map((node) => {
const groupAPIName = pascalCase(node.name);
const groupAPIName = toPascalCase(node.name);
return `${node.name}: ${groupAPIName}APIType,`;
}).join("\n");

Expand Down Expand Up @@ -120,7 +121,7 @@ const getTestCode = (api: APIMethodNode) => {
visitMethodNodes(api);

return `
import { assertEquals } from "../../dev_deps.ts";
import { assertEquals } from "@std/assert";
import { SlackAPI } from "../../mod.ts";

Deno.test("SlackAPIMethodsType generated types", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Slack API Client for Deno Run on Slack projects

```ts
import { SlackAPI } from "https://deno.land/x/[email protected]/mod.ts";
import { SlackAPI } from "jsr:@slack/api";

const client = SlackAPI(token);

Expand Down
8 changes: 6 additions & 2 deletions src/api-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { BaseSlackAPIClient } from "./base-client.ts";
import { BaseResponse, SlackAPIClient, SlackAPIMethodArgs } from "./types.ts";
import type { BaseSlackAPIClient } from "./base-client.ts";
import type {
BaseResponse,
SlackAPIClient,
SlackAPIMethodArgs,
} from "./types.ts";

const DO_NOT_PROXY = ["then"];

Expand Down
4 changes: 2 additions & 2 deletions src/api-proxy_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { APIProxy } from "./api-proxy.ts";
import { BaseSlackAPIClient } from "./base-client.ts";
import { SlackAPIMethodArgs } from "./types.ts";
import { assertSpyCall, spy } from "./dev_deps.ts";
import type { SlackAPIMethodArgs } from "./types.ts";
import { assertSpyCall, spy } from "@std/testing/mock";

Deno.test("APIProxy", async (t) => {
const baseClient = new BaseSlackAPIClient("test-token");
Expand Down
47 changes: 16 additions & 31 deletions src/api_test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { mf } from "./dev_deps.ts";
import {
assertEquals,
assertExists,
assertInstanceOf,
assertRejects,
isHttpError,
mf,
} from "./dev_deps.ts";
} from "@std/assert";
import { SlackAPI } from "./mod.ts";
import { HttpError } from "./deps.ts";
import { HttpError } from "@std/http/http-errors";

Deno.test("SlackAPI class", async (t) => {
mf.install(); // mock out calls to `fetch`
Expand Down Expand Up @@ -73,19 +72,13 @@ Deno.test("SlackAPI class", async (t) => {
});
});

await assertRejects(
async () => {
return await client.apiCall("chat.postMessage", {});
},
(error: Error) => {
assertInstanceOf(error, HttpError);
if (isHttpError(error)) {
assertEquals(error.headers?.get("Retry-After"), "120");
assertEquals(error.status, 429);
assertEquals(error.message, "429: ratelimited");
}
},
const error = await assertRejects(
() => client.apiCall("chat.postMessage", {}),
HttpError,
"429: ratelimited",
);
assertEquals(error.headers?.get("Retry-After"), "120");
assertEquals(error.status, 429);

mf.reset();
},
Expand Down Expand Up @@ -142,22 +135,14 @@ Deno.test("SlackAPI class", async (t) => {
});
});

await assertRejects(
async () => {
return await client.response(
"https://slack.com/api/chat.postMessage",
{},
);
},
(error: Error) => {
assertInstanceOf(error, HttpError);
if (isHttpError(error)) {
assertEquals(error.headers?.get("Retry-After"), "120");
assertEquals(error.status, 429);
assertEquals(error.message, "429: ratelimited");
}
},
const error = await assertRejects(
() =>
client.response("https://slack.com/api/chat.postMessage", {}),
HttpError,
);
assertEquals(error.headers?.get("Retry-After"), "120");
assertEquals(error.status, 429);
assertEquals(error.message, "429: ratelimited");

mf.reset();
},
Expand Down
4 changes: 2 additions & 2 deletions src/base-client-helpers_test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { assertEquals } from "@std/assert";
import {
_internals,
getUserAgent,
serializeData,
} from "./base-client-helpers.ts";
import { assertSpyCalls, stub } from "./dev_deps.ts";
import { assertSpyCalls, stub } from "@std/testing/mock";

Deno.test(`base-client-helpers.${_internals.getModuleVersion.name}`, async (t) => {
await t.step(
Expand Down
4 changes: 2 additions & 2 deletions src/base-client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
import type {
BaseResponse,
BaseSlackClient,
SlackAPIMethodArgs,
SlackAPIOptions,
} from "./types.ts";
import { createHttpError, HttpError } from "./deps.ts";
import { createHttpError, type HttpError } from "@std/http/http-errors";
import { getUserAgent, serializeData } from "./base-client-helpers.ts";

export class BaseSlackAPIClient implements BaseSlackClient {
Expand Down
7 changes: 1 addition & 6 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
export { pascalCase } from "https://deno.land/x/[email protected]/mod.ts";
export { emptyDir, ensureDir } from "https://deno.land/[email protected]/fs/mod.ts";
export {
createHttpError,
HttpError,
} from "https://deno.land/[email protected]/http/http_errors.ts";
export { pascalCase } from "jsr:@wok/[email protected]";
16 changes: 0 additions & 16 deletions src/dev_deps.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1 @@
export { assertSpyCall, spy } from "https://deno.land/x/[email protected]/mod.ts";
export {
assertEquals,
assertExists,
assertInstanceOf,
assertRejects,
} from "https://deno.land/[email protected]/testing/asserts.ts";
export * as mf from "https://deno.land/x/[email protected]/mod.ts";
export { isHttpError } from "https://deno.land/[email protected]/http/http_errors.ts";
export {
afterEach,
beforeAll,
} from "https://deno.land/[email protected]/testing/bdd.ts";
export {
assertSpyCalls,
stub,
} from "https://deno.land/[email protected]/testing/mock.ts";
2 changes: 1 addition & 1 deletion src/generated/method-types/api_method_types_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "../../dev_deps.ts";
import { assertEquals } from "@std/assert";
import { SlackAPI } from "../../mod.ts";

Deno.test("SlackAPIMethodsType generated types", () => {
Expand Down
Loading
Loading