Action!

Copilot, "how can i automate publishing my types with github actions?"

Copilot gave me a list of things! I'm not entirely sure my publish.yaml is correct though.

name: Publish Types Package

on:
  push:
    branches:
      - main

jobs:
  build-and-publish:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'
          registry-url: 'https://registry.npmjs.org'

      - name: Install dependencies
        run: npm install

      - name: Build types
        run: npm run build

      - name: Publish package
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

I'm using node 23.2.0 locally so let's update that.

I don't have my branch set to main so let's update that.

And I think I'm gonna have to finagle my swagger spec generation since it currently saves the file when server is started. Let's see if we can start server.

Copilot, "update workflow to start server"

name: Publish Types Package

on:
  push:
    branches:
      - master

jobs:
  build-and-publish:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'
          registry-url: 'https://registry.npmjs.org'

      - name: Install dependencies
        run: npm install

      - name: Build types
        run: npm run build:types

      - name: Start server
        run: npm run start:prod &

      - name: Wait for server to start
        run: sleep 10

      - name: Publish package
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

I updated it slightly again since we need to build our types after we have the server started.

If we try to start server then we need the database connection working in actions. This is starting to feel like I gave a mouse a cookie

Copilot, "set up github actino to include my postgres db"

Now we're being terse! I get some updates to my .yaml file, but getting the env and secrets wasn't called out explicitly here like it was earlier. Good thing we have short term memory.

Aaaaaand we publish the repo instead of the types!

Commit b0c74497120c97691bce0e502c7a1b25251dc5b9

Copilot, "how publish this nested types package"

There's not a ton to say here so I'll just add in my commit links.

Commit 9381e05c48516592e3dbc389a0913048f50a1e76

Copilot, "update github action to build ./types packiage"

Commit 8214cfb3a0cde51e9965224f7a287b1dbe1449fb

npm notice Publishing to https://registry.npmjs.org/ with tag latest and public access
npm error code E404
npm error 404 Not Found - PUT https://registry.npmjs.org/@types%2fscavenger-hunt-server - Not found```

Maybe this is how I scoped my package. It looks like I need to add my username to internal package name.

Commit 9844d678d5dd9d8acc5625c3a502f97939650eac

It takes around around a minute to see how I failed. Would love to speed this up, but I don't want to get bogged down right now.

TODO: speed up GitHub Actions testing.

Okay, it worked! @jon-garrett/scavenger-hunt-server is my types package! It's not named how I want it, but we're a step closer so now let's test out loading our Hunt type on the front end!

(Okay, that part doesn't work. lol)

Copilot, "can't load types package Cannot find module '@jon-garrett/scavenger-hunt-server' or its corresponding type declarations."

It may be that I didn't specify my types field correctly for internal types package. Now I'm wondering if I just need a types field in my main package to shortcut some of this. We'll circle back around to clean up after we can figure that out.

Commit eedf3b89c9b9b78d34a74399d7671eb9ccb38c18

Need to update version...

Commit 0caa750397f0b26db4ac458f5a08d65cc8c69a83

All right. No errors that run, but did it publish how we needed it to?

Load it:

import { components } from '@jon-garrett/scavenger-hunt-server'

type Hunt = components['schemas']['Hunt']
const saveHuntStepStub = (data: Hunt) => {
  console.log('data:', data)
}

Try to use it:

Image of function calling our stub and showing the type error

Full error for the curious:

Argument of type '{ location: string; clue: string; solution: string; }' is not assignable to parameter of type
'{ answer: {
    exact: boolean;
    type: Record<string, never>;
    value: string;
    };
    clue: string;
    description: string;
    id: number;
    latitude: number;
    longitude: number;
    name: string;
    }';
    Type '{ location: string; clue: string; solution: string; }' is missing the following properties from type '{
    answer: {
        exact: boolean;
        type: Record<string, never>;
        value: string;
    };
    clue: string;
    description: string;
    id: number;
    latitude: number;
    longitude: number;
    name: string;
    }':
    answer, description, id, latitude, and 2 more.
    ts(2345)

We get the error we expected!

This isn't the cleanest way to access the types so maybe we can refine that later.

type Hunt = HuntsApi.components['schemas']['Hunt']

I think at this point we may need to dig into the openapi-typescript documentation if we want further customization.