Skip to content

Commit

Permalink
Refactor: globally use process.env.BASE_ID
Browse files Browse the repository at this point in the history
Refactors all URLs in the app to use `process.env.BASE_ID` instead of
manually specifying the Airtable app ID, in-case it ever needs to
change.

In reality, this is just kind of a warm-up to the codebase.
  • Loading branch information
DecDuck committed Jan 27, 2025
1 parent 0541bee commit e7a6b16
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 16 deletions.
1 change: 1 addition & 0 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ AIRTABLE_API_KEY=REPLACE_ME
AIRTABLE_ENDPOINT_URL="https://middleman.hackclub.com/airtable"
AUTH_SECRET=REPLACE_ME
BASE_ID="appTeNFYcUiYfGcR6"
YSWS_BASE="app3A5kJwYqxMLOgh"
CRON_SECRET=AUTOMATICALLY_ASSIGNED_IN_PROD
EDGE_CONFIG=AUTOMATICALLY_ASSIGNED_IN_PROD
GITHUB_CLIENT_ID=REPLACE_ME
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/cron/process-background-jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async function processPendingPersonInitJobs() {
}
console.log(JSON.stringify(upsertBody, null, 2))
const result = await fetch(
'https://middleman.hackclub.com/airtable/v0/appTeNFYcUiYfGcR6/tblfTzYVqvDJlIYUB',
`https://middleman.hackclub.com/airtable/v0/${process.env.BASE_ID}/tblfTzYVqvDJlIYUB`,
{
cache: 'no-cache',
method: 'PATCH',
Expand Down
7 changes: 4 additions & 3 deletions src/app/api/cron/ysws-updates.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { orThrow } from '@/lib/utils'
import { withLock } from '../../../../lib/redis-lock'
import Airtable from 'airtable'

Expand All @@ -7,19 +8,19 @@ function and(...args: string[]) {
return `AND(${args.join(',')})`
}

function uniq(arr) {
function uniq<T>(arr: Array<T>) {
return Array.from(new Set(arr))
}

const base = new Airtable({
apiKey: process.env.AIRTABLE_API_KEY,
endpointUrl: process.env.AIRTABLE_ENDPOINT_URL,
}).base('appTeNFYcUiYfGcR6')
}).base(process.env.BASE_ID ?? orThrow("No BASE_ID set in environment"))

const yswsBase = new Airtable({
apiKey: process.env.AIRTABLE_API_KEY,
endpointUrl: process.env.AIRTABLE_ENDPOINT_URL,
}).base('app3A5kJwYqxMLOgh')
}).base(process.env.YSWS_BASE ?? orThrow("No YSWS_BASE set in environment"))

async function createNewShipChains(): Promise<void> {
await withLock('create-ship-chains', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/harbor/battles/fraud-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function sendFraudReport(
const session = await getSession()

const res = await fetch(
'https://middleman.hackclub.com/airtable/v0/appTeNFYcUiYfGcR6/flagged_projects',
`https://middleman.hackclub.com/airtable/v0/${process.env.BASE_ID}/flagged_projects`,
{
method: 'POST',
headers: {
Expand Down
2 changes: 1 addition & 1 deletion src/app/harbor/gallery/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function getShips(offset: string | undefined): Promise<{
offset: string | undefined
}> {
const res = await fetch(
`https://middleman.hackclub.com/airtable/v0/appTeNFYcUiYfGcR6/ships?view=Grid%20view${offset ? `&offset=${offset}` : ''}`,
`https://middleman.hackclub.com/airtable/v0/${process.env.BASE_ID}/ships?view=Grid%20view${offset ? `&offset=${offset}` : ''}`,
{
headers: {
Authorization: `Bearer ${process.env.AIRTABLE_API_KEY}`,
Expand Down
3 changes: 2 additions & 1 deletion src/app/slack-error/report-error.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use server'

import { orThrow } from '@/lib/utils'
import Airtable from 'airtable'

export const reportError = async (e: string) => {
console.log('reporting error')
const base = new Airtable({
apiKey: process.env.AIRTABLE_API_KEY,
endpointUrl: process.env.AIRTABLE_ENDPOINT_URL,
}).base('appTeNFYcUiYfGcR6')
}).base(process.env.BASE_ID ?? orThrow("No BASE_ID set"))

base('non_user_in_slack').create(
[
Expand Down
4 changes: 2 additions & 2 deletions src/app/utils/airtable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function reportTourStep(tourStepId: string) {
throw err
}

await fetch('https://api.airtable.com/v0/appTeNFYcUiYfGcR6/people', {
await fetch(`https://api.airtable.com/v0/${process.env.BASE_ID}/people`, {
method: 'PATCH',
headers: {
Authorization: `Bearer ${process.env.AIRTABLE_API_KEY}`,
Expand Down Expand Up @@ -94,7 +94,7 @@ export async function reportLeaderboardParticipating(participating: boolean) {

try {
const response = await fetch(
'https://api.airtable.com/v0/appTeNFYcUiYfGcR6/people',
`https://api.airtable.com/v0/${process.env.BASE_ID}/people`,
{
method: 'PATCH',
headers: {
Expand Down
2 changes: 2 additions & 0 deletions src/app/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,5 @@ export async function impersonate(slackId: string) {

await signAndSet(session)
}

export type { HsSession }
4 changes: 2 additions & 2 deletions src/app/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export async function fetchShips(
{ship_status} != 'deleted'
)`

let url = `https://middleman.hackclub.com/airtable/v0/appTeNFYcUiYfGcR6/ships?filterByFormula=${encodeURIComponent(
let url = `https://middleman.hackclub.com/airtable/v0/${process.env.BASE_ID}/ships?filterByFormula=${encodeURIComponent(
filterFormula,
)}`
if (maxRecords != null) url += `&maxRecords=${maxRecords}`
Expand Down Expand Up @@ -179,7 +179,7 @@ export async function getBestShips(): Promise<BestShip[]> {
console.log('Best ships MISS')

const recordPromise = await fetch(
'https://middleman.hackclub.com/airtable/v0/appTeNFYcUiYfGcR6/tblHeGZNG00d4GBBV?limit=3&view=viwHvRRLCwPMOmRhj',
`https://middleman.hackclub.com/airtable/v0/${process.env.BASE_ID}/tblHeGZNG00d4GBBV?limit=3&view=viwHvRRLCwPMOmRhj`,
{
headers: {
Authorization: `Bearer ${process.env.AIRTABLE_API_KEY}`,
Expand Down
2 changes: 1 addition & 1 deletion src/app/utils/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export async function createSlackSession(slackOpenidToken: string) {

// Let's create a Person record
const result = await fetch(
'https://middleman.hackclub.com/airtable/v0/appTeNFYcUiYfGcR6/people',
`https://middleman.hackclub.com/airtable/v0/${process.env.BASE_ID}/people`,
{
method: 'POST',
headers: {
Expand Down
6 changes: 3 additions & 3 deletions src/app/utils/server/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function person(): Promise<any> {
console.log('Person cache MISS')

const recordPromise = fetch(
`https://middleman.hackclub.com/airtable/v0/appTeNFYcUiYfGcR6/people/${session.personId}`,
`https://middleman.hackclub.com/airtable/v0/${process.env.BASE_ID}/people/${session.personId}`,
{
headers: {
Authorization: `Bearer ${process.env.AIRTABLE_API_KEY}`,
Expand Down Expand Up @@ -134,7 +134,7 @@ export interface SignpostFeedItem {
}
export async function fetchSignpostFeed(): Promise<SignpostFeedItem[]> {
const result = await fetch(
'https://middleman.hackclub.com/airtable/v0/appTeNFYcUiYfGcR6/signpost',
`https://middleman.hackclub.com/airtable/v0/${process.env.BASE_ID}/signpost`,
{
headers: {
Authorization: `Bearer ${process.env.AIRTABLE_API_KEY}`,
Expand Down Expand Up @@ -189,7 +189,7 @@ export interface ShopItem {
}
export async function fetchShopItems(): Promise<ShopItem[]> {
const result = await fetch(
'https://middleman.hackclub.com/airtable/v0/appTeNFYcUiYfGcR6/shop_items',
`https://middleman.hackclub.com/airtable/v0/${process.env.BASE_ID}/shop_items`,
{
headers: {
Authorization: `Bearer ${process.env.AIRTABLE_API_KEY}`,
Expand Down
2 changes: 1 addition & 1 deletion src/app/utils/waka.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface WakaInfo {

// // Add to person record
// const res = await fetch(
// `https://middleman.hackclub.com/airtable/v0/appTeNFYcUiYfGcR6/people`,
// `https://middleman.hackclub.com/airtable/v0/${process.env.BASE_ID}/people`,
// {
// method: "PATCH",
// headers: {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ import { twMerge } from 'tailwind-merge'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}

export function orThrow(message?: string): never {
throw new Error(message ?? 'An error occurred.')
}

0 comments on commit e7a6b16

Please sign in to comment.