Skip to content

Commit

Permalink
First version of @dmno/cloudflare-platform (#161)
Browse files Browse the repository at this point in the history
* move signup api to cloudflare pages functions and refactor associated code

* force a preview deploy

* testing functions

* unclean

* cloudflare platform and dwrangler

* final cleanup

---------

Co-authored-by: Theo Ephraim <[email protected]>
  • Loading branch information
philmillman and theoephraim authored Dec 17, 2024
1 parent 82d2179 commit dc2425c
Show file tree
Hide file tree
Showing 40 changed files with 2,135 additions and 325 deletions.
9 changes: 9 additions & 0 deletions .changeset/soft-terms-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@dmno/cloudflare-platform": patch
"@dmno/nextjs-integration": patch
"@dmno/astro-integration": patch
"@dmno/remix-integration": patch
"dmno": patch
---

cloudflare platform support
2 changes: 1 addition & 1 deletion .dmno/config.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DmnoBaseTypes, defineDmnoService } from 'dmno';
import { DmnoBaseTypes, defineDmnoService, pickFromSchemaObject } from 'dmno';
import { GitDataTypes, GithubDataTypes } from 'dmno/vendor-types';

export default defineDmnoService({
Expand Down
5 changes: 5 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
"import": "./dist/globals-injector-standalone/edge/injector.js",
"default": "./dist/globals-injector-standalone/edge/injector.cjs"
},
"./injector-standalone/edge-auto": {
"ts-src": "./src/globals-injector/auto-inject.ts",
"import": "./dist/globals-injector-standalone/edge/auto-inject.js",
"default": "./dist/globals-injector-standalone/edge/auto-inject.cjs"
},
"./tsconfigs/*.json": "./tsconfigs/*",
"./tsconfigs/*": "./tsconfigs/*.json"
},
Expand Down
49 changes: 36 additions & 13 deletions packages/core/src/config-engine/authoring-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,54 @@ import { DmnoDataTypeMetadata } from './configraph-adapter';

// export function pickFromSchemaObject<S extends Record<string, any>>(schemaObj: S, keys: Array<keyof S>);
// export function pickFromSchemaObject<S extends Record<string, any>>(schemaObj: S, ...keys: Array<keyof S>);
export function pickFromSchemaObject<S extends Record<string, any>>(
export function pickFromSchemaObject<
S extends Record<string, ConfigraphDataTypeDefinition<unknown, DmnoDataTypeMetadata>>,
>(
schemaObj: S,
firstKeyOrKeyArray: keyof S | Array<keyof S>,
pickSettings: (
keyof S |
Array<keyof S> |
Partial<Record<keyof S, ConfigraphDataTypeDefinition<unknown, DmnoDataTypeMetadata>>>
),
...keysRest: Array<keyof S>
) {
const keyArray = _.isArray(firstKeyOrKeyArray) ? firstKeyOrKeyArray : [firstKeyOrKeyArray, ...keysRest];
return Object.fromEntries(keyArray.map((k) => [k, schemaObj[k]]));
): Partial<Record<keyof S, ConfigraphDataTypeDefinition<unknown, DmnoDataTypeMetadata>>> {
if (_.isArray(pickSettings) || _.isString(pickSettings)) {
const keyArray = _.isArray(pickSettings)
? pickSettings
: [pickSettings, ...keysRest];
return _.pick(schemaObj, keyArray);
} else if (_.isObject(pickSettings)) {
return _.mapValues(pickSettings, (itemOverrides, key) => {
return {
...schemaObj[key],
...itemOverrides,
};
});
} else {
throw new Error('Invalid pick from schema');
}
}


export function createVendorSchema(
schema: Record<string, ConfigraphDataTypeDefinitionOrShorthand<DmnoDataTypeMetadata>>,
export function createVendorSchema<
S extends Record<string, ConfigraphDataTypeDefinitionOrShorthand<DmnoDataTypeMetadata>>,
>(
schema: S,
commonTraits?: Partial<ConfigraphDataTypeDefinition<unknown, DmnoDataTypeMetadata>>,
) {
if (!commonTraits) return schema;
): Record< keyof S, ConfigraphDataTypeDefinition<unknown, DmnoDataTypeMetadata>> {
return _.mapValues(schema, (item, _itemKey) => {
if (item instanceof ConfigraphDataType || _.isString(item) || _.isFunction(item)) {
return {
extends: item,
...commonTraits,
};
} else {
return {
// TODO: not sure why I need this any :(
...item as any,
...commonTraits,
};
}
return {
...item,
...commonTraits,
};
// throw Error('invalid vendor schema');
});
}
2 changes: 2 additions & 0 deletions packages/core/src/config-loader/vite-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export async function setupViteServer(opts: {
},

transform(code, id, options) {
// TODO: need to detect file types more broadly and can do better detection of if we need to inject at all
if (id.endsWith('.json')) return;
// fairly naive way of doing this... but for now we are replacing `DMNO_CONFIG.SOME_KEY` with `getResolverCtx().get('SOME_KEY')`
// TODO: we probably should limit which files this applies in
const fixedCode = new MagicString(code);
Expand Down
13 changes: 12 additions & 1 deletion packages/core/src/globals-injector/auto-inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@

import { injectDmnoGlobals as _injectDmnoGlobals } from './injector';

_injectDmnoGlobals();
let defineInjectedConfig;
try {
// we'll attempt to inject data from a global/replaced var of __DMNO_INJECTED_CONFIG__
// this is used in something like the cloudflare dwrangler integration, where we use an esbuild replacement
// otherwise we call injectDmnoGlobals() with nothing, and it will look for an env var `DMNO_INJECTED_ENV`
// which would come come something like `dmno run`

// @ts-ignore
defineInjectedConfig = __DMNO_INJECTED_CONFIG__;
} catch (err) {}
if (defineInjectedConfig) _injectDmnoGlobals({ injectedConfig: defineInjectedConfig });
else _injectDmnoGlobals();

export const injectDmnoGlobals = _injectDmnoGlobals;

1 change: 1 addition & 0 deletions packages/core/tsup.inject-standalone.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const outDir = `dist/globals-injector-standalone${process.env.DMNO_EDGE_COMPAT ?
export default defineConfig({
entry: [ // Entry point(s)
'src/globals-injector/injector.ts', // function used to inject dmno globals
'src/globals-injector/auto-inject.ts', // exports inject function and automatically calls it once
],

esbuildPlugins: [
Expand Down
4 changes: 4 additions & 0 deletions packages/docs-site/astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ export default defineConfig({
label: 'Overview',
link: '/docs/platforms/overview/',
},
{
label: 'Cloudflare',
link: '/docs/platforms/cloudflare/',
},
{
label: 'Netlify',
link: '/docs/platforms/netlify/',
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion packages/docs-site/src/components/EmailSignup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<input v-if="isSubmitting" placeholder="Sending..." />
<template v-else>
<input type="text" v-model="email" :placeholder="emailSubmitted ? 'Thanks!' : 'Your email'" />
<a href="#" @click="onSubmit" class="button" v-html="ArrowIconSvg">
<a href="#" @click.prevent="onSubmit" class="button" v-html="ArrowIconSvg">
</a>
</template>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,17 @@ Even if you are not writing TypeScript, these days you are still likely relying
// ...
"include": [
"**/*.js",
+ ".dmno/.typegen/global.d.ts"
+ ".dmno/.typegen/global.d.ts",
+ ".dmno/.typegen/global-public.d.ts"
]
}
```

:::note[Invalid keys are not strict]
JavaScript is by default not strict - so while you will get nice autocompletion and IntelliSense on your config, your IDE will not give you the handy red squiggle if you use an _invalid_ key. Depending on your setup you may still get a build/runtime error.
:::



#### Injecting `DMNO_CONFIG` vs `DMNO_PUBLIC_CONFIG`

Expand Down
Loading

0 comments on commit dc2425c

Please sign in to comment.