Skip to content

Commit

Permalink
style: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Create or Update Pull Request Action authored and gr2m committed Mar 22, 2020
1 parent 9dfcf92 commit 71e1d1d
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 247 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const auth = createAppAuth({
privateKey: "-----BEGIN PRIVATE KEY-----\n...",
installationId: 123,
clientId: "1234567890abcdef1234",
clientSecret: "1234567890abcdef12341234567890abcdef1234"
clientSecret: "1234567890abcdef12341234567890abcdef1234",
});

// Retrieve JSON Web Token (JWT) to authenticate as app
Expand Down Expand Up @@ -197,8 +197,8 @@ createAppAuth({
clientId: 123,
clientSecret: "secret",
request: request.defaults({
baseUrl: "https://ghe.my-company.com/api/v3"
})
baseUrl: "https://ghe.my-company.com/api/v3",
}),
});
```

Expand All @@ -224,8 +224,8 @@ createAppAuth({
},
async set(key, value) {
CACHE[key] = value;
}
}
},
},
});
```

Expand Down Expand Up @@ -621,8 +621,8 @@ Or it can be passed as option to [`request()`](https://github.com/octokit/reques
```js
const requestWithAuth = request.defaults({
request: {
hook: auth.hook
}
hook: auth.hook,
},
});

const { data: installations } = await requestWithAuth("GET /app/installations");
Expand All @@ -633,12 +633,12 @@ Note that `auth.hook()` does not create and set an OAuth authentication token. B
```js
const { token } = await auth({
type: "oauth",
code: "123456"
code: "123456",
});
const requestWithAuth = request.defaults({
headers: {
authentication: `token ${token}`
}
authentication: `token ${token}`,
},
});
```

Expand Down
2 changes: 1 addition & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
AuthOptions,
StrategyOptionsWithDefaults,
Authentication
Authentication,
} from "./types";
import { getAppAuthentication } from "./get-app-authentication";
import { getInstallationAuthentication } from "./get-installation-authentication";
Expand Down
18 changes: 10 additions & 8 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import {
CacheData,
Permissions,
InstallationAccessTokenData,
REPOSITORY_SELECTION
REPOSITORY_SELECTION,
} from "./types";

export function getCache() {
return new LRU<number, string>({
// cache max. 15000 tokens, that will use less than 10mb memory
max: 15000,
// Cache for 1 minute less than GitHub expiry
maxAge: 1000 * 60 * 59
maxAge: 1000 * 60 * 59,
});
}

Expand All @@ -36,7 +36,7 @@ export async function get(
expiresAt,
repositorySelection,
permissionsString,
singleFileName
singleFileName,
] = result.split("|");

const permissions =
Expand All @@ -57,7 +57,7 @@ export async function get(
permissions,
repositoryIds: options.repositoryIds,
singleFileName,
repositorySelection: repositorySelection as REPOSITORY_SELECTION
repositorySelection: repositorySelection as REPOSITORY_SELECTION,
};
}
export async function set(
Expand All @@ -70,7 +70,9 @@ export async function set(
const permissionsString = options.permissions
? ""
: Object.keys(data.permissions)
.map(name => `${name}${data.permissions[name] === "write" ? "!" : ""}`)
.map(
(name) => `${name}${data.permissions[name] === "write" ? "!" : ""}`
)
.join(",");

await cache.set(
Expand All @@ -80,7 +82,7 @@ export async function set(
data.expiresAt,
data.repositorySelection,
permissionsString,
data.singleFileName
data.singleFileName,
]
.join("|")
.replace(/\|+$/, "")
Expand All @@ -90,11 +92,11 @@ export async function set(
function optionsToCacheKey({
installationId,
permissions = {},
repositoryIds = []
repositoryIds = [],
}: InstallationAuthOptions) {
const permissionsString = Object.keys(permissions)
.sort()
.map(name => (permissions[name] === "read" ? name : `${name}!`))
.map((name) => (permissions[name] === "read" ? name : `${name}!`))
.join(",");

const repositoryIdsString = repositoryIds.sort().join(",");
Expand Down
2 changes: 1 addition & 1 deletion src/get-app-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export async function getAppAuthentication(
type: "app",
token: appAuthentication.token,
appId: appAuthentication.appId,
expiresAt: new Date(appAuthentication.expiration * 1000).toISOString()
expiresAt: new Date(appAuthentication.expiration * 1000).toISOString(),
};
}
20 changes: 10 additions & 10 deletions src/get-installation-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
RequestInterface,
InstallationAuthOptions,
StrategyOptionsWithDefaults,
InstallationAccessTokenAuthentication
InstallationAccessTokenAuthentication,
} from "./types";

export async function getInstallationAuthentication(
Expand All @@ -25,7 +25,7 @@ export async function getInstallationAuthentication(
permissions,
repositoryIds,
singleFileName,
repositorySelection
repositorySelection,
} = result;

return toTokenAuthentication({
Expand All @@ -35,7 +35,7 @@ export async function getInstallationAuthentication(
permissions,
repositorySelection,
repositoryIds,
singleFileName
singleFileName,
});
}
}
Expand All @@ -53,18 +53,18 @@ export async function getInstallationAuthentication(
repositories,
permissions,
repository_selection: repositorySelection,
single_file: singleFileName
}
single_file: singleFileName,
},
} = await request("POST /app/installations/:installation_id/access_tokens", {
installation_id: installationId,
repository_ids: options.repositoryIds,
permissions: options.permissions,
mediaType: {
previews: ["machine-man"]
previews: ["machine-man"],
},
headers: {
authorization: `bearer ${appAuthentication.token}`
}
authorization: `bearer ${appAuthentication.token}`,
},
});

const repositoryIds = repositories
Expand All @@ -77,7 +77,7 @@ export async function getInstallationAuthentication(
repositorySelection,
permissions,
repositoryIds,
singleFileName
singleFileName,
});

return toTokenAuthentication({
Expand All @@ -87,6 +87,6 @@ export async function getInstallationAuthentication(
repositorySelection,
permissions,
repositoryIds,
singleFileName
singleFileName,
});
}
12 changes: 6 additions & 6 deletions src/get-oauth-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
RequestInterface,
OAuthOptions,
StrategyOptionsWithDefaults,
OAuthAccesTokenAuthentication
OAuthAccesTokenAuthentication,
} from "./types";
import { RequestError } from "@octokit/request-error";

Expand All @@ -26,13 +26,13 @@ export async function getOAuthAuthentication(

const parameters = {
headers: {
accept: `application/json`
accept: `application/json`,
},
client_id: state.clientId,
client_secret: state.clientSecret,
code: options.code,
state: options.state,
redirect_uri: options.redirectUrl
redirect_uri: options.redirectUrl,
};

const response = await request(route, parameters);
Expand All @@ -43,19 +43,19 @@ export async function getOAuthAuthentication(
response.status,
{
headers: response.headers,
request: request.endpoint(route, parameters)
request: request.endpoint(route, parameters),
}
);
}

const {
data: { access_token: token, scope }
data: { access_token: token, scope },
} = response;

return {
type: "token",
tokenType: "oauth",
token,
scopes: scope.split(/,\s*/).filter(Boolean)
scopes: scope.split(/,\s*/).filter(Boolean),
};
}
2 changes: 1 addition & 1 deletion src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
RequestParameters,
RequestInterface,
Route,
State
State,
} from "./types";

export async function hook(
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
State,
StrategyOptions,
AuthOptions,
Authentication
Authentication,
} from "./types";
import { VERSION } from "./version";

Expand All @@ -26,15 +26,15 @@ export const createAppAuth: StrategyInterface = function createAppAuth(
{
request: request.defaults({
headers: {
"user-agent": `octokit-auth-app.js/${VERSION} ${getUserAgent()}`
}
"user-agent": `octokit-auth-app.js/${VERSION} ${getUserAgent()}`,
},
}),
cache: getCache()
cache: getCache(),
},
options
);

return Object.assign(auth.bind(null, state), {
hook: hook.bind(null, state)
hook: hook.bind(null, state),
});
};
8 changes: 4 additions & 4 deletions src/requires-app-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const PATHS = [
"/repos/:owner/:repo/installation",
"/repos/:owner/:repo/installation",
"/users/:username/installation",
"/users/:username/installation"
"/users/:username/installation",
];

// CREDIT: Simon Grondin (https://github.com/SGrondin)
Expand All @@ -21,10 +21,10 @@ function routeMatcher(paths: string[]) {
"/repos/:owner/:repo/collaborators/:username"
] */

const regexes = paths.map(p =>
const regexes = paths.map((p) =>
p
.split("/")
.map(c => (c.startsWith(":") ? "(?:.+?)" : c))
.map((c) => (c.startsWith(":") ? "(?:.+?)" : c))
.join("/")
);
// 'regexes' would contain:
Expand All @@ -33,7 +33,7 @@ function routeMatcher(paths: string[]) {
'/repos/(?:.+?)/(?:.+?)/collaborators/(?:.+?)'
] */

const regex = `^(?:${regexes.map(r => `(?:${r})`).join("|")})[^/]*$`;
const regex = `^(?:${regexes.map((r) => `(?:${r})`).join("|")})[^/]*$`;
// 'regex' would contain:
/*
^(?:(?:\/orgs\/(?:.+?)\/invitations)|(?:\/repos\/(?:.+?)\/(?:.+?)\/collaborators\/(?:.+?)))[^\/]*$
Expand Down
6 changes: 3 additions & 3 deletions src/to-token-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
InstallationAccessTokenAuthentication,
WithInstallationId,
TOKEN_TYPE,
INSTALLATION_TOKEN_TYPE
INSTALLATION_TOKEN_TYPE,
} from "./types";

export function toTokenAuthentication({
Expand All @@ -13,7 +13,7 @@ export function toTokenAuthentication({
repositorySelection,
permissions,
repositoryIds,
singleFileName
singleFileName,
}: CacheData & WithInstallationId): InstallationAccessTokenAuthentication {
return Object.assign(
{
Expand All @@ -23,7 +23,7 @@ export function toTokenAuthentication({
installationId,
permissions,
expiresAt,
repositorySelection
repositorySelection,
},
repositoryIds ? { repositoryIds } : null,
singleFileName ? { singleFileName } : null
Expand Down
Loading

0 comments on commit 71e1d1d

Please sign in to comment.