Skip to content

Commit

Permalink
feat: improve alpha types support (#9256)
Browse files Browse the repository at this point in the history
* chore: make return types explicit

* chore: improve module processing

* fix lint
  • Loading branch information
runspired authored Mar 12, 2024
1 parent 7127828 commit dc83f4e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 20 deletions.
13 changes: 8 additions & 5 deletions packages/model/src/-private/legacy-relationships-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type { CollectionRelationship } from '@warp-drive/core-types/cache/relati
import type { LocalRelationshipOperation } from '@warp-drive/core-types/graph';
import type { CollectionResourceRelationship, SingleResourceRelationship } from '@warp-drive/core-types/spec/raw';

import type { ManyArray } from '../-private';
import RelatedCollection from './many-array';
import type { MinimalLegacyRecord } from './model-methods';
import type { BelongsToProxyCreateArgs, BelongsToProxyMeta } from './promise-belongs-to';
Expand Down Expand Up @@ -298,11 +299,11 @@ export class LegacySupport {
assert('hasMany only works with the @ember-data/json-api package');
}

reloadHasMany(key: string, options?: BaseFinderOptions) {
reloadHasMany<T>(key: string, options?: BaseFinderOptions): Promise<ManyArray<T>> | PromiseManyArray<T> {
if (HAS_JSON_API_PACKAGE) {
const loadingPromise = this._relationshipPromisesCache[key];
if (loadingPromise) {
return loadingPromise;
return loadingPromise as Promise<ManyArray<T>>;
}
const relationship = this.graph.get(this.identifier, key) as CollectionEdge;
const { definition, state } = relationship;
Expand All @@ -313,10 +314,10 @@ export class LegacySupport {
const promise = this.fetchAsyncHasMany(key, relationship, manyArray, options);

if (this._relationshipProxyCache[key]) {
return this._updatePromiseProxyFor('hasMany', key, { promise });
return this._updatePromiseProxyFor('hasMany', key, { promise }) as PromiseManyArray<T>;
}

return promise;
return promise as Promise<ManyArray<T>>;
}
assert(`hasMany only works with the @ember-data/json-api package`);
}
Expand Down Expand Up @@ -388,7 +389,9 @@ export class LegacySupport {
return promiseProxy;
}

referenceFor(kind: string | null, name: string) {
referenceFor(kind: 'belongsTo', name: string): BelongsToReference;
referenceFor(kind: 'hasMany', name: string): HasManyReference;
referenceFor(kind: 'belongsTo' | 'hasMany', name: string) {
let reference = this.references[name];

if (!reference) {
Expand Down
8 changes: 4 additions & 4 deletions packages/model/src/-private/many-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,10 @@ export default class RelatedCollection<T = unknown> extends RecordArray<T> {
/**
Reloads all of the records in the manyArray. If the manyArray
holds a relationship that was originally fetched using a links url
Ember Data will revisit the original links url to repopulate the
EmberData will revisit the original links url to repopulate the
relationship.
If the manyArray holds the result of a `store.query()` reload will
If the ManyArray holds the result of a `store.query()` reload will
re-run the original query.
Example
Expand All @@ -409,9 +409,9 @@ export default class RelatedCollection<T = unknown> extends RecordArray<T> {
@method reload
@public
*/
reload(options?: BaseFinderOptions) {
reload(options?: BaseFinderOptions): Promise<this> {
// TODO this is odd, we don't ask the store for anything else like this?
return this._manager.reloadHasMany(this.key, options);
return this._manager.reloadHasMany<T>(this.key, options) as Promise<this>;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions packages/model/src/-private/model-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { RecordStore } from '@warp-drive/core-types/symbols';
import type Errors from './errors';
import { lookupLegacySupport } from './legacy-relationships-support';
import type RecordState from './record-state';
import type BelongsToReference from './references/belongs-to';
import type HasManyReference from './references/has-many';

export interface MinimalLegacyRecord {
errors: Errors;
Expand Down Expand Up @@ -51,11 +53,11 @@ export function unloadRecord(this: MinimalLegacyRecord) {
this[RecordStore].unloadRecord(this);
}

export function belongsTo(this: MinimalLegacyRecord, prop: string) {
export function belongsTo(this: MinimalLegacyRecord, prop: string): BelongsToReference {
return lookupLegacySupport(this).referenceFor('belongsTo', prop);
}

export function hasMany(this: MinimalLegacyRecord, prop: string) {
export function hasMany(this: MinimalLegacyRecord, prop: string): HasManyReference {
return lookupLegacySupport(this).referenceFor('hasMany', prop);
}

Expand Down
5 changes: 2 additions & 3 deletions packages/model/src/-private/promise-belongs-to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type { TypeFromInstanceOrString } from '@warp-drive/core-types/record';

import type { LegacySupport } from './legacy-relationships-support';
import { PromiseObject } from './promise-proxy-base';
import type BelongsToReference from './references/belongs-to';

export interface BelongsToProxyMeta<T = unknown> {
key: string;
Expand Down Expand Up @@ -51,9 +50,9 @@ class PromiseBelongsTo<T = unknown> extends Extended<T> {
declare _belongsToState: BelongsToProxyMeta<T>;

@cached
get id() {
get id(): string | null {
const { key, legacySupport } = this._belongsToState;
const ref = legacySupport.referenceFor('belongsTo', key) as BelongsToReference;
const ref = legacySupport.referenceFor('belongsTo', key);

return ref.id();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/model/src/-private/references/has-many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -743,11 +743,11 @@ export default class HasManyReference<
@param {Object} options the options to pass in.
@return {Promise} a promise that resolves with the ManyArray in this has-many relationship.
*/
reload(options?: BaseFinderOptions) {
reload(options?: BaseFinderOptions): Promise<ManyArray<Related>> {
const support: LegacySupport = (LEGACY_SUPPORT as Map<StableRecordIdentifier, LegacySupport>).get(
this.___identifier
)!;
return support.reloadHasMany(this.key, options);
return support.reloadHasMany(this.key, options) as Promise<ManyArray<Related>>;
}
}
defineSignal(HasManyReference.prototype, '_ref', 0);
Expand Down
11 changes: 7 additions & 4 deletions release/core/publish/steps/generate-tarballs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { APPLIED_STRATEGY, Package } from '../../../utils/package';
import path from 'path';
import fs from 'fs';
import { Glob } from 'bun';
import { getFile } from '../../../utils/json-file';

const PROJECT_ROOT = process.cwd();
const TARBALL_DIR = path.join(PROJECT_ROOT, 'tmp/tarballs');
Expand Down Expand Up @@ -176,9 +175,13 @@ async function convertFileToModule(fileData: string, relativePath: string, pkgNa
lines[i] = lines[i].replace(/^declare /, '').replaceAll(' declare ', ' ');
const line = lines[i];

if (line.includes(`import(".`) || line.includes(`import('.`)) {
throw new Error(`Unhandled Dynamic Relative Import in ${relativePath}`);
}

if (line.startsWith('import ')) {
if (!line.includes(`'`)) {
throw new Error(`Unhandled import in ${relativePath}`);
throw new Error(`Unhandled Import in ${relativePath}`);
}
if (line.includes(`'.`)) {
const importPath = line.match(/'([^']+)'/)![1];
Expand All @@ -190,7 +193,7 @@ async function convertFileToModule(fileData: string, relativePath: string, pkgNa
// fix re-exports
else if (line.startsWith('export {')) {
if (!line.includes('}')) {
throw new Error(`Unhandled re-export in ${relativePath}`);
throw new Error(`Unhandled Re-export in ${relativePath}`);
}
if (line.includes(`'.`)) {
const importPath = line.match(/'([^']+)'/)![1];
Expand All @@ -202,7 +205,7 @@ async function convertFileToModule(fileData: string, relativePath: string, pkgNa
// fix * re-exports
else if (line.startsWith('export * from')) {
if (!line.includes(`'`)) {
throw new Error(`Unhandled re-export in ${relativePath}`);
throw new Error(`Unhandled Re-export in ${relativePath}`);
}
if (line.includes(`'.`)) {
const importPath = line.match(/'([^']+)'/)![1];
Expand Down

0 comments on commit dc83f4e

Please sign in to comment.