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

build(types): add --exactOptionalPropertyTypes option #647

Closed
Closed
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
8 changes: 4 additions & 4 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"lint:fix": "prettier --write '{src,test,scripts}/**/*.{ts,md}' README.md *.json",
"pretest": "npm run -s lint",
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage",
"test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --esModuleInterop --strict --target es2022 --module node16 --moduleResolution node16 test/typescript-validate.ts"
"test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --esModuleInterop --strict --target es2022 --module node16 --moduleResolution node16 --exactOptionalPropertyTypes test/typescript-validate.ts"
},
"repository": "github:octokit/auth-app.js",
"keywords": [
Expand All @@ -35,7 +35,7 @@
"universal-user-agent": "^7.0.0"
},
"devDependencies": {
"@octokit/tsconfig": "^3.0.0",
"@octokit/tsconfig": "^4.0.0",
"@types/fetch-mock": "^7.3.1",
"@types/jest": "^29.0.0",
"@types/node": "^20.0.0",
Expand Down
15 changes: 11 additions & 4 deletions src/get-app-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ export async function getAppAuthentication({
privateKey,
timeDifference,
}: State & {
timeDifference?: number;
timeDifference?: number | undefined;
}): Promise<AppAuthentication> {
try {
const appAuthentication = await githubAppJwt({
const authOptions = {
id: appId,
privateKey,
now: timeDifference && Math.floor(Date.now() / 1000) + timeDifference,
});
};

if (timeDifference) {
Object.assign(authOptions, {
now: Math.floor(Date.now() / 1000) + timeDifference,
});
}

const appAuthentication = await githubAppJwt(authOptions);

return {
type: "app",
Expand Down
63 changes: 45 additions & 18 deletions src/get-installation-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export async function getInstallationAuthentication(
state.cache,
optionsWithInstallationTokenFromState,
);

if (result) {
const {
token,
Expand Down Expand Up @@ -69,6 +70,30 @@ export async function getInstallationAuthentication(
const appAuthentication = await getAppAuthentication(state);
const request = customRequest || state.request;

const payload = {
installation_id: installationId,
mediaType: {
previews: ["machine-man"],
},
headers: {
authorization: `bearer ${appAuthentication.token}`,
},
};

if (options.repositoryIds) {
Object.assign(payload, { repository_ids: options.repositoryIds });
}

if (options.repositoryNames) {
Object.assign(payload, {
repositories: options.repositoryNames,
});
}

if (options.permissions) {
Object.assign(payload, { permissions: options.permissions });
}

const {
data: {
token,
Expand All @@ -78,18 +103,10 @@ export async function getInstallationAuthentication(
repository_selection: repositorySelectionOptional,
single_file: singleFileName,
},
} = await request("POST /app/installations/{installation_id}/access_tokens", {
installation_id: installationId,
repository_ids: options.repositoryIds,
repositories: options.repositoryNames,
permissions: options.permissions,
mediaType: {
previews: ["machine-man"],
},
headers: {
authorization: `bearer ${appAuthentication.token}`,
},
});
} = await request(
"POST /app/installations/{installation_id}/access_tokens",
payload,
);

/* istanbul ignore next - permissions are optional per OpenAPI spec, but we think that is incorrect */
const permissions = permissionsOptional || {};
Expand All @@ -105,18 +122,23 @@ export async function getInstallationAuthentication(
: void 0;

const createdAt = new Date().toISOString();
await set(state.cache, optionsWithInstallationTokenFromState, {
const cacheOptions = {
token,
createdAt,
expiresAt,
repositorySelection,
permissions,
repositoryIds,
repositoryNames,
singleFileName,
});
};

if (singleFileName) {
Object.assign(payload, { singleFileName });
}

await set(state.cache, optionsWithInstallationTokenFromState, cacheOptions);

return toTokenAuthentication({
const cacheData = {
installationId,
token,
createdAt,
Expand All @@ -125,6 +147,11 @@ export async function getInstallationAuthentication(
permissions,
repositoryIds,
repositoryNames,
singleFileName,
});
};

if (singleFileName) {
Object.assign(cacheData, { singleFileName });
}

return toTokenAuthentication(cacheData);
}
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ export type InstallationAccessTokenData = {
expiresAt: UTC_TIMESTAMP;
permissions: Permissions;
repositorySelection: REPOSITORY_SELECTION;
repositoryIds?: number[];
repositoryNames?: string[];
singleFileName?: string;
repositoryIds?: number[] | undefined;
repositoryNames?: string[] | undefined;
singleFileName?: string | undefined;
};

export type CacheData = InstallationAccessTokenData;
Expand Down